From bdff1c3f0213f564c451c230883be6da90814c6c Mon Sep 17 00:00:00 2001 From: Douglas Lowder Date: Thu, 27 Oct 2016 01:37:06 -0700 Subject: [PATCH] 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 --- .../RCTLoggingTests.m | 24 ++++++++++++------- IntegrationTests/LoggingTestModule.js | 5 ++++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/Examples/UIExplorer/UIExplorerIntegrationTests/RCTLoggingTests.m b/Examples/UIExplorer/UIExplorerIntegrationTests/RCTLoggingTests.m index 866a14f3a..5f299c02b 100644 --- a/Examples/UIExplorer/UIExplorerIntegrationTests/RCTLoggingTests.m +++ b/Examples/UIExplorer/UIExplorerIntegrationTests/RCTLoggingTests.m @@ -51,14 +51,6 @@ XCTAssertFalse(_bridge.loading); _logSem = dispatch_semaphore_create(0); - 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); - } - }); } - (void)tearDown @@ -71,7 +63,21 @@ - (void)testLogging { - [_bridge enqueueJSCall:@"LoggingTestModule.logToConsole" args:@[@"Invoking console.log"]]; + // 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); diff --git a/IntegrationTests/LoggingTestModule.js b/IntegrationTests/LoggingTestModule.js index cb801a0af..ec07f3271 100644 --- a/IntegrationTests/LoggingTestModule.js +++ b/IntegrationTests/LoggingTestModule.js @@ -19,6 +19,11 @@ var LoggingTestModule = { logToConsole: function(str) { console.log(str); }, + logToConsoleAfterWait: function(str,timeout_ms) { + setTimeout(function() { + console.log(str); + }, timeout_ms); + }, warning: function(str) { warning(false, str); },