mirror of
https://github.com/status-im/react-native.git
synced 2025-01-17 13:01:13 +00:00
02b6e38bee
Summary:D3092867 / 1d3db4c5dc8763d16f2d051fdf04a2976c0fb154 caused deadlock when chrome debugging was turned on, so it was reverted as D3128586 / 144dc3066144a48fc13bb7832abc9e645024fb88. The reason: I was calling `[_bridge dispatchBlock:^{ [self flushEventsQueue]; } queue:RCTJSThread];` from main thread and expecting it will `dispatch_async` to another, since a held lock was being accessed the dispatched block and was released after the dispatch. Turns out `RCTWebSocketExecutor` (which is used when chrome debugger is turned on) executes all blocks dispatched this way to `RCTJSThread` synchronously on the main thread. This resulted in a deadlock. The "dispatched" block was trying to acquired lock which held by the same thread in the dispatching phase. A fix for this is pretty simple. We will release the lock before dispatching the block. However it's not super straightforward to see this won't introduce some race condition in a case with two threads where we would end up with events not being processed. My thinking why that shouldn't happen goes like this: We could get in a bad state if `flushEventsQueue` would run on JS thread while `sendEvent:` is running on MT. (I don't have a specific example how, maybe it's not possible. However when I show this case is safe we know we are good.) The way how locking is setup in this diff the only possible scenario where these two threads would execute in these methods concurrently is JS holding the lock and MT going to enqueue another block on JS thread (since that's outside of "locked" zone). But this scenarion can never happen, since if MT is about to enqueue the block on JS thread it means there cannot be a not yet fully executed block on JS thread. Therefore nothing bad can happen. So this diff brings back the reverted diff and adds to it the fix for the deadlock. Reviewed By: javache Differential Revision: D3130375 fb-gh-sync-id: 885a166f2f808551d7cd4e4eb98634d26afe6a11 fbshipit-source-id: 885a166f2f808551d7cd4e4eb98634d26afe6a11
251 lines
7.5 KiB
Objective-C
251 lines
7.5 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 <UIKit/UIKit.h>
|
|
#import <XCTest/XCTest.h>
|
|
|
|
#import <OCMock/OCMock.h>
|
|
#import "RCTEventDispatcher.h"
|
|
#import "RCTBridge+Private.h"
|
|
|
|
@interface RCTTestEvent : NSObject <RCTEvent>
|
|
@property (atomic, assign, readwrite) BOOL canCoalesce;
|
|
@end
|
|
|
|
@implementation RCTTestEvent
|
|
{
|
|
NSDictionary<NSString *, id> *_body;
|
|
}
|
|
|
|
@synthesize viewTag = _viewTag;
|
|
@synthesize eventName = _eventName;
|
|
@synthesize coalescingKey = _coalescingKey;
|
|
|
|
- (instancetype)initWithViewTag:(NSNumber *)viewTag
|
|
eventName:(NSString *)eventName
|
|
body:(NSDictionary<NSString *, id> *)body
|
|
coalescingKey:(uint16_t)coalescingKey
|
|
{
|
|
if (self = [super init]) {
|
|
_viewTag = viewTag;
|
|
_eventName = eventName;
|
|
_body = body;
|
|
_canCoalesce = YES;
|
|
_coalescingKey = coalescingKey;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (id<RCTEvent>)coalesceWithEvent:(id<RCTEvent>)newEvent
|
|
{
|
|
return newEvent;
|
|
}
|
|
|
|
+ (NSString *)moduleDotMethod
|
|
{
|
|
return @"RCTDeviceEventEmitter.emit";
|
|
}
|
|
|
|
- (NSArray *)arguments
|
|
{
|
|
return @[_eventName, _body];
|
|
}
|
|
|
|
@end
|
|
|
|
@interface RCTEventDispatcherTests : XCTestCase
|
|
@end
|
|
|
|
@implementation RCTEventDispatcherTests
|
|
{
|
|
id _bridge;
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
NSString *_eventName;
|
|
NSDictionary<NSString *, id> *_body;
|
|
RCTTestEvent *_testEvent;
|
|
NSString *_JSMethod;
|
|
}
|
|
|
|
|
|
- (void)setUp
|
|
{
|
|
[super setUp];
|
|
|
|
_bridge = [OCMockObject mockForClass:[RCTBatchedBridge class]];
|
|
|
|
_eventDispatcher = [RCTEventDispatcher new];
|
|
[_eventDispatcher setValue:_bridge forKey:@"bridge"];
|
|
|
|
_eventName = RCTNormalizeInputEventName(@"sampleEvent");
|
|
_body = @{ @"foo": @"bar" };
|
|
_testEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
eventName:_eventName
|
|
body:_body
|
|
coalescingKey:0];
|
|
|
|
_JSMethod = [[_testEvent class] moduleDotMethod];
|
|
}
|
|
|
|
- (void)testLegacyEventsAreImmediatelyDispatched
|
|
{
|
|
[[_bridge expect] enqueueJSCall:_JSMethod
|
|
args:[_testEvent arguments]];
|
|
|
|
[_eventDispatcher sendDeviceEventWithName:_eventName body:_body];
|
|
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testNonCoalescingEventIsImmediatelyDispatched
|
|
{
|
|
_testEvent.canCoalesce = NO;
|
|
|
|
[[_bridge expect] dispatchBlock:OCMOCK_ANY queue:RCTJSThread];
|
|
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testCoalescingEventIsImmediatelyDispatched
|
|
{
|
|
_testEvent.canCoalesce = YES;
|
|
|
|
[[_bridge expect] dispatchBlock:OCMOCK_ANY queue:RCTJSThread];
|
|
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testMultipleEventsResultInOnlyOneDispatchAfterTheFirstOne
|
|
{
|
|
[[_bridge expect] dispatchBlock:OCMOCK_ANY queue:RCTJSThread];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testRunningTheDispatchedBlockResultInANewOneBeingEnqueued
|
|
{
|
|
__block dispatch_block_t eventsEmittingBlock;
|
|
[[_bridge expect] dispatchBlock:[OCMArg checkWithBlock:^(dispatch_block_t block) {
|
|
eventsEmittingBlock = block;
|
|
return YES;
|
|
}] queue:RCTJSThread];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_bridge verify];
|
|
|
|
|
|
// eventsEmittingBlock would be called when js is no longer busy, which will result in emitting events
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:[_testEvent arguments]];
|
|
eventsEmittingBlock();
|
|
[_bridge verify];
|
|
|
|
|
|
[[_bridge expect] dispatchBlock:OCMOCK_ANY queue:RCTJSThread];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testBasicCoalescingReturnsLastEvent
|
|
{
|
|
__block dispatch_block_t eventsEmittingBlock;
|
|
[[_bridge expect] dispatchBlock:[OCMArg checkWithBlock:^(dispatch_block_t block) {
|
|
eventsEmittingBlock = block;
|
|
return YES;
|
|
}] queue:RCTJSThread];
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:[_testEvent arguments]];
|
|
|
|
RCTTestEvent *ignoredEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
eventName:_eventName
|
|
body:@{ @"other": @"body" }
|
|
coalescingKey:0];
|
|
[_eventDispatcher sendEvent:ignoredEvent];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
eventsEmittingBlock();
|
|
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testDifferentEventTypesDontCoalesce
|
|
{
|
|
NSString *firstEventName = RCTNormalizeInputEventName(@"firstEvent");
|
|
RCTTestEvent *firstEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
eventName:firstEventName
|
|
body:_body
|
|
coalescingKey:0];
|
|
|
|
__block dispatch_block_t eventsEmittingBlock;
|
|
[[_bridge expect] dispatchBlock:[OCMArg checkWithBlock:^(dispatch_block_t block) {
|
|
eventsEmittingBlock = block;
|
|
return YES;
|
|
}] queue:RCTJSThread];
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:[firstEvent arguments]];
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:[_testEvent arguments]];
|
|
|
|
|
|
[_eventDispatcher sendEvent:firstEvent];
|
|
[_eventDispatcher sendEvent:_testEvent];
|
|
eventsEmittingBlock();
|
|
|
|
[_bridge verify];
|
|
}
|
|
|
|
- (void)testSameEventTypesWithDifferentCoalesceKeysDontCoalesce
|
|
{
|
|
NSString *eventName = RCTNormalizeInputEventName(@"firstEvent");
|
|
RCTTestEvent *firstEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
eventName:eventName
|
|
body:_body
|
|
coalescingKey:0];
|
|
RCTTestEvent *secondEvent = [[RCTTestEvent alloc] initWithViewTag:nil
|
|
eventName:eventName
|
|
body:_body
|
|
coalescingKey:1];
|
|
|
|
__block dispatch_block_t eventsEmittingBlock;
|
|
[[_bridge expect] dispatchBlock:[OCMArg checkWithBlock:^(dispatch_block_t block) {
|
|
eventsEmittingBlock = block;
|
|
return YES;
|
|
}] queue:RCTJSThread];
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:[firstEvent arguments]];
|
|
[[_bridge expect] enqueueJSCall:@"RCTDeviceEventEmitter.emit"
|
|
args:[secondEvent arguments]];
|
|
|
|
|
|
[_eventDispatcher sendEvent:firstEvent];
|
|
[_eventDispatcher sendEvent:secondEvent];
|
|
[_eventDispatcher sendEvent:firstEvent];
|
|
[_eventDispatcher sendEvent:secondEvent];
|
|
[_eventDispatcher sendEvent:secondEvent];
|
|
[_eventDispatcher sendEvent:firstEvent];
|
|
[_eventDispatcher sendEvent:firstEvent];
|
|
|
|
eventsEmittingBlock();
|
|
|
|
[_bridge verify];
|
|
}
|
|
|
|
@end
|