Skip to content

Commit

Permalink
fix(COActorMessage.m): fix actor not return error
Browse files Browse the repository at this point in the history
fix actor not return error

fix alibaba#78
  • Loading branch information
pengyutang125 committed May 7, 2019
1 parent d3331df commit c0da711
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ static dispatch_queue_t get_test_queue(){
return q;
}

static COPromise *test_promise(){
return [COPromise promise:^(COPromiseFulfill _Nonnull fullfill, COPromiseReject _Nonnull reject) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
reject([NSError errorWithDomain:@"test" code:100 userInfo:nil]);
});
}];
}

SpecBegin(coActor)

describe(@"actor tests", ^{
Expand Down Expand Up @@ -248,6 +256,26 @@ static dispatch_queue_t get_test_queue(){
int currentCount = [await([countActor sendMessage:@"get"]) intValue];
});
});

it(@"error example", ^{
COActor *actor = co_actor_onqueue(get_test_queue(), ^(COActorChan *channel) {
for(COActorMessage *message in channel){
message.complete(await(test_promise()));
}
});
co_launch(^{
id value = await([actor sendMessage:@"test"]);
NSError *error = co_getError();
XCTAssert(error.code == 100);
});

waitUntilTimeout(3, ^(DoneCallback done) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
done();
});
});

});
});

SpecEnd
15 changes: 14 additions & 1 deletion coobjc/actor/COActorMessage.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#import "COActorMessage.h"

extern NSError *co_getError(void);

@interface COActorMessage ()

@property(nonatomic, strong) COActorCompletable *completableObj;
Expand All @@ -40,7 +42,18 @@ - (instancetype)initWithType:(id)type
COActorCompletable *completable = _completableObj;
return ^(id val){
if (completable) {
[completable fulfill:val];
if (val) {
[completable fulfill:val];
}
else{
NSError *error = co_getError();
if (error) {
[completable reject:error];
}
else{
[completable fulfill:val];
}
}
}
};
}
Expand Down

0 comments on commit c0da711

Please sign in to comment.