Skip to content

Commit

Permalink
Merge pull request #246 from ably/history-until-attach
Browse files Browse the repository at this point in the history
RTL10b: Implement untilAttach for RealtimeChannel.history.
  • Loading branch information
tcard committed Feb 24, 2016
2 parents 909c77f + 44e90f4 commit 03a7d16
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
7 changes: 7 additions & 0 deletions ably-ios/ARTDataQuery+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "ARTDataQuery.h"
#import "ARTRealtimeChannel.h"

ART_ASSUME_NONNULL_BEGIN

Expand All @@ -16,4 +17,10 @@ ART_ASSUME_NONNULL_BEGIN

@end

@interface ARTRealtimeHistoryQuery ()

@property (weak, readwrite) ARTRealtimeChannel *realtimeChannel;

@end

ART_ASSUME_NONNULL_END
13 changes: 13 additions & 0 deletions ably-ios/ARTDataQuery.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "ARTDataQuery+Private.h"
#import "ARTRealtimeChannel+Private.h"

@implementation ARTDataQuery

Expand Down Expand Up @@ -49,4 +50,16 @@ - (NSMutableArray *)asQueryItems {

@implementation ARTRealtimeHistoryQuery

- (NSMutableArray *)asQueryItems {
NSMutableArray *items = [super asQueryItems];
if (self.untilAttach) {
NSAssert(self.realtimeChannel, @"ARTRealtimeHistoryQuery used from outside ARTRealitmeChannel.history");
if (self.realtimeChannel.state != ARTRealtimeChannelAttached) {
@throw [NSError errorWithDomain:ARTAblyErrorDomain code:ARTRealtimeHistoryErrorNotAttached userInfo:nil];
}
[items addObject:[NSURLQueryItem queryItemWithName:@"fromSerial" value:self.realtimeChannel.attachSerial]];
}
return items;
}

@end
12 changes: 11 additions & 1 deletion ably-ios/ARTRealtimeChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "ARTRealtimeChannel+Private.h"
#import "ARTChannel+Private.h"
#import "ARTDataQuery+Private.h"

#import "ARTRealtime+Private.h"
#import "ARTMessage.h"
Expand Down Expand Up @@ -533,7 +534,16 @@ - (void)history:(void (^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *, NSError
}

- (BOOL)history:(ARTRealtimeHistoryQuery *)query callback:(void (^)(__GENERIC(ARTPaginatedResult, ARTMessage *) *, NSError *))callback error:(NSError **)errorPtr {
return [super history:query callback:callback error:errorPtr];
query.realtimeChannel = self;
@try {
return [super history:query callback:callback error:errorPtr];
}
@catch (NSError *error) {
if (errorPtr) {
*errorPtr = error;
}
return false;
}
}

@end
26 changes: 13 additions & 13 deletions ablySpec/RealtimeClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,6 @@ class RealtimeClientChannel: QuickSpec {

// RTL10
context("history") {

// RTL10a
it("should support all the same params as Rest") {
let options = AblyTests.commonAppSetup()
Expand Down Expand Up @@ -1249,7 +1248,7 @@ class RealtimeClientChannel: QuickSpec {
}

// RTL10b
pending("supports the param untilAttach") {
context("supports the param untilAttach") {

it("should be false as default") {
let query = ARTRealtimeHistoryQuery()
Expand All @@ -1267,11 +1266,11 @@ class RealtimeClientChannel: QuickSpec {
do {
try channel.history(query, callback: { _, _ in })
}
catch ARTRealtimeHistoryError.NotAttached {
return
}
catch {
fail("Shouldn't raise a global error")
catch let error as NSError {
if error.code == ARTRealtimeHistoryError.NotAttached.rawValue {
return
}
fail("Shouldn't raise a global error, got \(error)")
}
fail("Should raise an error")
}
Expand All @@ -1283,7 +1282,7 @@ class RealtimeClientChannel: QuickSpec {
let cases = [CaseTest(untilAttach: true), CaseTest(untilAttach: false)]

for caseItem in cases {
it("where value is \(caseItem.untilAttach), should pass the querystring param from_serial with the serial number assigned to the channel") {
it("where value is \(caseItem.untilAttach), should pass the querystring param fromSerial with the serial number assigned to the channel") {
let client = ARTRealtime(options: AblyTests.commonAppSetup())
defer { client.close() }
let channel = client.channels.get("test")
Expand All @@ -1294,6 +1293,7 @@ class RealtimeClientChannel: QuickSpec {
let query = ARTRealtimeHistoryQuery()
query.untilAttach = caseItem.untilAttach

channel.attach()
expect(channel.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)

waitUntil(timeout: testTimeout) { done in
Expand All @@ -1306,10 +1306,10 @@ class RealtimeClientChannel: QuickSpec {
let queryString = mockExecutor.requests.last!.URL!.query

if query.untilAttach {
expect(queryString).to(contain("from_serial=\(channel.attachSerial)"))
expect(queryString).to(contain("fromSerial=\(channel.attachSerial!)"))
}
else {
expect(queryString).toNot(contain("from_serial"))
expect(queryString).toNot(contain("fromSerial"))
}
}
}
Expand Down Expand Up @@ -1339,7 +1339,7 @@ class RealtimeClientChannel: QuickSpec {
}

client2.connect()
let channel2 = client1.channels.get("test")
let channel2 = client2.channels.get("test")
channel2.attach()
expect(channel2.state).toEventually(equal(ARTRealtimeChannelState.Attached), timeout: testTimeout)

Expand Down Expand Up @@ -1367,8 +1367,8 @@ class RealtimeClientChannel: QuickSpec {
try! channel2.history(query) { result, errorInfo in
expect(result!.items).to(haveCount(20))
expect(result!.hasNext).to(beFalse())
expect((result!.items.first! as! ARTMessage).data as? String).to(equal("message 19"))
expect((result!.items.last! as! ARTMessage).data as? String).to(equal("message 0"))
expect((result!.items.first as? ARTMessage)?.data as? String).to(equal("message 19"))
expect((result!.items.last as? ARTMessage)?.data as? String).to(equal("message 0"))
done()
}
}
Expand Down

0 comments on commit 03a7d16

Please sign in to comment.