Douglas Lowder bdff1c3f02 Fix timing issues in RCTLoggingTests.m
Summary:
**Motivation**

If there are any console log messages that come in on initialization (as will happen right now in tvOS), the RCTLoggingTests can fail intermittently.  This change delays the start of the logging test to allow time for initial console messages to come in.
Closes https://github.com/facebook/react-native/pull/10568

Differential Revision: D4087974

Pulled By: bestander

fbshipit-source-id: 2b0f4a88a74bc6121133317dd909d5bd1f10789b
2016-10-27 01:43:34 -07:00

127 lines
4.6 KiB
Objective-C

/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <XCTest/XCTest.h>
#import "RCTAssert.h"
#import "RCTLog.h"
#import "RCTBridge.h"
@interface RCTLoggingTests : XCTestCase
@end
@implementation RCTLoggingTests
{
RCTBridge *_bridge;
dispatch_semaphore_t _logSem;
RCTLogLevel _lastLogLevel;
RCTLogSource _lastLogSource;
NSString *_lastLogMessage;
}
- (void)setUp
{
NSURL *scriptURL;
if (getenv("CI_USE_PACKAGER")) {
NSString *app = @"IntegrationTests/IntegrationTestsApp";
scriptURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8081/%@.bundle?platform=ios&dev=true", app]];
} else {
scriptURL = [[NSBundle bundleForClass:[RCTBridge class]] URLForResource:@"main" withExtension:@"jsbundle"];
}
RCTAssert(scriptURL != nil, @"No scriptURL set");
_bridge = [[RCTBridge alloc] initWithBundleURL:scriptURL moduleProvider:NULL launchOptions:nil];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60];
while (date.timeIntervalSinceNow > 0 && _bridge.loading) {
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}
XCTAssertFalse(_bridge.loading);
_logSem = dispatch_semaphore_create(0);
}
- (void)tearDown
{
[_bridge invalidate];
_bridge = nil;
RCTSetLogFunction(RCTDefaultLogFunction);
}
- (void)testLogging
{
// First console log call will fire after 2.0 sec, to allow for any initial log messages
// that might come in (seeing this in tvOS)
[_bridge enqueueJSCall:@"LoggingTestModule.logToConsoleAfterWait" args:@[@"Invoking console.log",@2000]];
// Spin native layer for 1.9 sec
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.9]];
// Now set the log function to signal the semaphore
RCTSetLogFunction(^(RCTLogLevel level, RCTLogSource source, __unused NSString *fileName, __unused NSNumber *lineNumber, NSString *message) {
if (source == RCTLogSourceJavaScript) {
self->_lastLogLevel = level;
self->_lastLogSource = source;
self->_lastLogMessage = message;
dispatch_semaphore_signal(self->_logSem);
}
});
// Wait for console log to signal the semaphore
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
XCTAssertEqual(_lastLogLevel, RCTLogLevelInfo);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.log");
[_bridge enqueueJSCall:@"LoggingTestModule.warning" args:@[@"Generating warning"]];
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
XCTAssertEqual(_lastLogLevel, RCTLogLevelWarning);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Warning: Generating warning");
[_bridge enqueueJSCall:@"LoggingTestModule.invariant" args:@[@"Invariant failed"]];
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invariant failed");
[_bridge enqueueJSCall:@"LoggingTestModule.logErrorToConsole" args:@[@"Invoking console.error"]];
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
// For local bundles, we'll first get a warning about symbolication
if ([_bridge.bundleURL isFileURL]) {
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
}
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Invoking console.error");
[_bridge enqueueJSCall:@"LoggingTestModule.throwError" args:@[@"Throwing an error"]];
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
// For local bundles, we'll first get a warning about symbolication
if ([_bridge.bundleURL isFileURL]) {
dispatch_semaphore_wait(_logSem, DISPATCH_TIME_FOREVER);
}
XCTAssertEqual(_lastLogLevel, RCTLogLevelError);
XCTAssertEqual(_lastLogSource, RCTLogSourceJavaScript);
XCTAssertEqualObjects(_lastLogMessage, @"Throwing an error");
}
@end