Skip to content

Commit 08a4d1c

Browse files
committed
change datafile manager test to make sure download works properly
1 parent a606637 commit 08a4d1c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

OptimizelySDKDatafileManager/OptimizelySDKDatafileManagerTests/OPTLYDatafileManagerTest.m

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,38 @@ - (void)testSaveDatafileMethod {
149149
XCTAssertEqualObjects(datafile, savedData, @"retrieved saved data from disk should be equivilent to the datafile we wanted to save to disk");
150150
}
151151

152-
- (void)testDatafileManagerPullsDatafileOnInitialization {
152+
- (void)testDatafileManagerDownloadDatafileSavesDatafile {
153+
154+
// instantiate datafile manager
155+
OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock:^(OPTLYDatafileManagerBuilder * _Nullable builder) {
156+
builder.projectId = kProjectId;
157+
}];
158+
XCTAssertNotNil(datafileManager);
159+
XCTAssertFalse([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile], @"no datafile sould exist yet.");
160+
153161
// setup stubbing and listener expectation
154162
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"testInitializeClientAsync"];
155163
id<OHHTTPStubsDescriptor> stub = [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
156164
return [request.URL.host isEqualToString:@"cdn.optimizely.com"];
157165
} withStubResponse:^OHHTTPStubsResponse*(NSURLRequest *request) {
158166
// Stub it with our "wsresponse.json" stub file (which is in same bundle as self)
159-
[expectation fulfill];
160167
return [OHHTTPStubsResponse responseWithData:[OPTLYTestHelper loadJSONDatafileIntoDataObject:kDatamodelDatafileName]
161168
statusCode:200
162169
headers:@{@"Content-Type":@"application/json"}];
163170
}];
164171

165-
// instantiate datafile manager (it should fire off a request)
166-
XCTAssertFalse([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile], @"no datafile sould exist yet.");
167-
OPTLYDatafileManager *datafileManager = [OPTLYDatafileManager initWithBuilderBlock:^(OPTLYDatafileManagerBuilder * _Nullable builder) {
168-
builder.projectId = kProjectId;
172+
// Call download datafile
173+
__block Boolean completionWasCalled = false;
174+
175+
[datafileManager downloadDatafile:datafileManager.projectId
176+
completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
177+
completionWasCalled = true;
178+
[expectation fulfill];
169179
}];
170-
XCTAssertNotNil(datafileManager);
171180

172181
// make sure we were able to save the datafile
173182
[self waitForExpectationsWithTimeout:2 handler:nil];
174-
sleep(2); // not sure if there is a better way for to wait for disk write other than to sleep this thread
183+
XCTAssertTrue(completionWasCalled);
175184
XCTAssertTrue([self.dataStore fileExists:kProjectId type:OPTLYDataStoreDataTypeDatafile], @"we should have stored the datafile");
176185

177186
// cleanup stubs

0 commit comments

Comments
 (0)