Skip to content

Commit 8bcc8a3

Browse files
authored
fix(storage): fix bug in storage operation tests (#2441)
1 parent 893f7d5 commit 8bcc8a3

8 files changed

+48
-43
lines changed

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageDownloadFileOperationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
4040

4141
operation.start()
4242
waitForExpectations(timeout: 1)
43-
XCTAssertTrue(operation.isFinished)
43+
waitForOperationToFinish(operation)
4444
}
4545

4646
func testDownloadFileOperationGetIdentityIdError() async throws {
@@ -69,7 +69,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
6969
operation.start()
7070

7171
await waitForExpectations(timeout: 1)
72-
XCTAssertTrue(operation.isFinished)
72+
waitForOperationToFinish(operation)
7373
}
7474

7575
func testDownloadFileOperationDownloadLocal() {
@@ -104,7 +104,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
104104
operation.start()
105105

106106
waitForExpectations(timeout: 1)
107-
XCTAssertTrue(operation.isFinished)
107+
waitForOperationToFinish(operation)
108108
mockStorageService.verifyDownload(serviceKey: expectedServiceKey, fileURL: url)
109109
}
110110

@@ -140,7 +140,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
140140
operation.start()
141141

142142
waitForExpectations(timeout: 1)
143-
XCTAssertTrue(operation.isFinished)
143+
waitForOperationToFinish(operation)
144144
mockStorageService.verifyDownload(serviceKey: expectedServiceKey, fileURL: url)
145145
}
146146

@@ -178,7 +178,7 @@ class AWSS3StorageDownloadFileOperationTests: AWSS3StorageOperationTestBase {
178178
operation.start()
179179

180180
await waitForExpectations(timeout: 1)
181-
XCTAssertTrue(operation.isFinished)
181+
waitForOperationToFinish(operation)
182182
mockStorageService.verifyDownload(serviceKey: expectedServiceKey, fileURL: url)
183183
}
184184

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageGetDataOperationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
3838

3939
operation.start()
4040
waitForExpectations(timeout: 1)
41-
XCTAssertTrue(operation.isFinished)
41+
waitForOperationToFinish(operation)
4242
}
4343

4444
func testDownloadDataOperationGetIdentityIdError() async throws {
@@ -65,7 +65,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
6565
operation.start()
6666

6767
await waitForExpectations(timeout: 1)
68-
XCTAssertTrue(operation.isFinished)
68+
waitForOperationToFinish(operation)
6969
}
7070

7171
func testDownloadDataOperationDownloadData() async throws {
@@ -97,7 +97,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
9797
operation.start()
9898

9999
await waitForExpectations(timeout: 1)
100-
XCTAssertTrue(operation.isFinished)
100+
waitForOperationToFinish(operation)
101101
mockStorageService.verifyDownload(serviceKey: expectedServiceKey, fileURL: nil)
102102
}
103103

@@ -130,7 +130,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
130130
operation.start()
131131

132132
await waitForExpectations(timeout: 1)
133-
XCTAssertTrue(operation.isFinished)
133+
waitForOperationToFinish(operation)
134134
mockStorageService.verifyDownload(serviceKey: expectedServiceKey, fileURL: nil)
135135
}
136136

@@ -165,7 +165,7 @@ class AWSS3StorageDownloadDataOperationTests: AWSS3StorageOperationTestBase {
165165
operation.start()
166166

167167
await waitForExpectations(timeout: 1)
168-
XCTAssertTrue(operation.isFinished)
168+
waitForOperationToFinish(operation)
169169
mockStorageService.verifyDownload(serviceKey: expectedServiceKey, fileURL: nil)
170170
}
171171

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageGetURLOperationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AWSS3StorageGetURLOperationTests: AWSS3StorageOperationTestBase {
3939

4040
operation.start()
4141
waitForExpectations(timeout: 1)
42-
XCTAssertTrue(operation.isFinished)
42+
waitForOperationToFinish(operation)
4343
}
4444

4545
func testGetURLOperationGetIdentityIdError() async throws {
@@ -68,7 +68,7 @@ class AWSS3StorageGetURLOperationTests: AWSS3StorageOperationTestBase {
6868
operation.start()
6969

7070
await waitForExpectations(timeout: 1)
71-
XCTAssertTrue(operation.isFinished)
71+
waitForOperationToFinish(operation)
7272
}
7373

7474
func testGetOperationGetPresignedURL() async throws {
@@ -96,7 +96,7 @@ class AWSS3StorageGetURLOperationTests: AWSS3StorageOperationTestBase {
9696

9797
operation.start()
9898
await waitForExpectations(timeout: 1)
99-
XCTAssertTrue(operation.isFinished)
99+
waitForOperationToFinish(operation)
100100
XCTAssertEqual(mockStorageService.getPreSignedURLCalled, 1)
101101
mockStorageService.verifyGetPreSignedURL(serviceKey: expectedServiceKey, expires: expectedExpires)
102102
}
@@ -126,7 +126,7 @@ class AWSS3StorageGetURLOperationTests: AWSS3StorageOperationTestBase {
126126

127127
operation.start()
128128
await waitForExpectations(timeout: 1)
129-
XCTAssertTrue(operation.isFinished)
129+
waitForOperationToFinish(operation)
130130
XCTAssertEqual(mockStorageService.getPreSignedURLCalled, 1)
131131
mockStorageService.verifyGetPreSignedURL(serviceKey: expectedServiceKey, expires: expectedExpires)
132132
}
@@ -154,7 +154,7 @@ class AWSS3StorageGetURLOperationTests: AWSS3StorageOperationTestBase {
154154

155155
operation.start()
156156
await waitForExpectations(timeout: 1)
157-
XCTAssertTrue(operation.isFinished)
157+
waitForOperationToFinish(operation)
158158
XCTAssertEqual(mockStorageService.getPreSignedURLCalled, 1)
159159
mockStorageService.verifyGetPreSignedURL(serviceKey: expectedServiceKey,
160160
expires: StorageGetURLRequest.Options.defaultExpireInSeconds)

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageListOperationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class AWSS3StorageListOperationTests: AWSS3StorageOperationTestBase {
4040
operation.start()
4141

4242
waitForExpectations(timeout: 1)
43-
XCTAssertTrue(operation.isFinished)
43+
waitForOperationToFinish(operation)
4444
}
4545

4646
func testListOperationGetIdentityIdError() {
@@ -69,7 +69,7 @@ class AWSS3StorageListOperationTests: AWSS3StorageOperationTestBase {
6969
operation.start()
7070

7171
waitForExpectations(timeout: 1)
72-
XCTAssertTrue(operation.isFinished)
72+
waitForOperationToFinish(operation)
7373
}
7474

7575
func testListOperationListObjects() {
@@ -94,7 +94,7 @@ class AWSS3StorageListOperationTests: AWSS3StorageOperationTestBase {
9494
operation.start()
9595

9696
waitForExpectations(timeout: 1)
97-
XCTAssertTrue(operation.isFinished)
97+
waitForOperationToFinish(operation)
9898
mockStorageService.verifyList(prefix: expectedPrefix, path: testPath)
9999
}
100100

@@ -120,7 +120,7 @@ class AWSS3StorageListOperationTests: AWSS3StorageOperationTestBase {
120120
operation.start()
121121

122122
waitForExpectations(timeout: 1)
123-
XCTAssertTrue(operation.isFinished)
123+
waitForOperationToFinish(operation)
124124
mockStorageService.verifyList(prefix: expectedPrefix, path: testPath)
125125
}
126126

@@ -148,7 +148,7 @@ class AWSS3StorageListOperationTests: AWSS3StorageOperationTestBase {
148148
operation.start()
149149

150150
waitForExpectations(timeout: 1)
151-
XCTAssertTrue(operation.isFinished)
151+
waitForOperationToFinish(operation)
152152
mockStorageService.verifyList(prefix: expectedPrefix, path: testPath)
153153
}
154154
}

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageOperationTestBase.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import XCTest
1313
import AWSS3
1414

1515
class AWSS3StorageOperationTestBase: XCTestCase {
16-
16+
1717
var hubPlugin: MockHubCategoryPlugin!
1818
var mockStorageService: MockAWSS3StorageService!
1919
var mockAuthService: MockAWSAuthService!
20-
20+
2121
let testKey = "TestKey"
2222
let testTargetIdentityId = "TestTargetIdentityId"
2323
let testIdentityId = "TestIdentityId"
@@ -27,24 +27,29 @@ class AWSS3StorageOperationTestBase: XCTestCase {
2727
let testExpires = 10
2828
let testURL = URL(fileURLWithPath: "path")
2929
let testStorageConfiguration = AWSS3StoragePluginConfiguration()
30-
30+
3131
override func setUp() {
3232
let mockAmplifyConfig = AmplifyConfiguration()
33-
33+
3434
do {
3535
try Amplify.configure(mockAmplifyConfig)
3636
} catch let error as AmplifyError {
3737
XCTFail("setUp failed with error: \(error); \(error.errorDescription); \(error.recoverySuggestion)")
3838
} catch {
3939
XCTFail("setup failed with unknown error")
4040
}
41-
41+
4242
mockStorageService = MockAWSS3StorageService()
4343
mockAuthService = MockAWSAuthService()
4444
}
45-
45+
4646
override func tearDown() async throws {
4747
await Amplify.reset()
4848
}
49-
49+
50+
func waitForOperationToFinish(_ operation: AsynchronousOperation) {
51+
while !operation.isFinished {
52+
Thread.sleep(forTimeInterval: 0.2)
53+
}
54+
}
5055
}

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StoragePutDataOperationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
4141
operation.start()
4242

4343
waitForExpectations(timeout: 1)
44-
XCTAssertTrue(operation.isFinished)
44+
waitForOperationToFinish(operation)
4545
}
4646

4747
func testUploadDataOperationGetIdentityIdError() {
@@ -71,7 +71,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
7171
operation.start()
7272

7373
waitForExpectations(timeout: 1)
74-
XCTAssertTrue(operation.isFinished)
74+
waitForOperationToFinish(operation)
7575
}
7676

7777
func testUploadDataOperationUploadSuccess() {
@@ -113,7 +113,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
113113
operation.start()
114114

115115
waitForExpectations(timeout: 1)
116-
XCTAssertTrue(operation.isFinished)
116+
waitForOperationToFinish(operation)
117117
XCTAssertEqual(mockStorageService.uploadCalled, 1)
118118
mockStorageService.verifyUpload(serviceKey: expectedServiceKey,
119119
key: testKey,
@@ -157,7 +157,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
157157
operation.start()
158158

159159
waitForExpectations(timeout: 1)
160-
XCTAssertTrue(operation.isFinished)
160+
waitForOperationToFinish(operation)
161161
XCTAssertEqual(mockStorageService.uploadCalled, 1)
162162
mockStorageService.verifyUpload(serviceKey: expectedServiceKey,
163163
key: testKey,
@@ -212,7 +212,7 @@ class AWSS3StorageUploadDataOperationTests: AWSS3StorageOperationTestBase {
212212
operation.start()
213213

214214
waitForExpectations(timeout: 1)
215-
XCTAssertTrue(operation.isFinished)
215+
waitForOperationToFinish(operation)
216216
XCTAssertEqual(mockStorageService.multiPartUploadCalled, 1)
217217
mockStorageService.verifyMultiPartUpload(serviceKey: expectedServiceKey,
218218
key: testKey,

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageRemoveOperationTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AWSS3StorageRemoveOperationTests: AWSS3StorageOperationTestBase {
3737

3838
operation.start()
3939

40-
XCTAssertTrue(operation.isFinished)
40+
waitForOperationToFinish(operation)
4141
waitForExpectations(timeout: 1)
4242
}
4343

@@ -65,7 +65,7 @@ class AWSS3StorageRemoveOperationTests: AWSS3StorageOperationTestBase {
6565

6666
operation.start()
6767
await waitForExpectations(timeout: 1)
68-
XCTAssertTrue(operation.isFinished)
68+
waitForOperationToFinish(operation)
6969
}
7070

7171
func testRemoveOperationDeleteSuccess() async throws {
@@ -90,7 +90,7 @@ class AWSS3StorageRemoveOperationTests: AWSS3StorageOperationTestBase {
9090
operation.start()
9191

9292
await waitForExpectations(timeout: 1)
93-
XCTAssertTrue(operation.isFinished)
93+
waitForOperationToFinish(operation)
9494
mockStorageService.verifyDelete(serviceKey: expectedServiceKey)
9595
}
9696

@@ -116,7 +116,7 @@ class AWSS3StorageRemoveOperationTests: AWSS3StorageOperationTestBase {
116116
operation.start()
117117

118118
await waitForExpectations(timeout: 1)
119-
XCTAssertTrue(operation.isFinished)
119+
waitForOperationToFinish(operation)
120120
mockStorageService.verifyDelete(serviceKey: expectedServiceKey)
121121
}
122122

@@ -143,7 +143,7 @@ class AWSS3StorageRemoveOperationTests: AWSS3StorageOperationTestBase {
143143
operation.start()
144144

145145
waitForExpectations(timeout: 1)
146-
XCTAssertTrue(operation.isFinished)
146+
waitForOperationToFinish(operation)
147147
mockStorageService.verifyDelete(serviceKey: expectedServiceKey)
148148
}
149149
}

AmplifyPlugins/Storage/Tests/AWSS3StoragePluginTests/Operation/AWSS3StorageUploadFileOperationTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
4141
operation.start()
4242

4343
waitForExpectations(timeout: 1)
44-
XCTAssertTrue(operation.isFinished)
44+
waitForOperationToFinish(operation)
4545
}
4646

4747
func testUploadFileOperationGetIdentityIdError() {
@@ -73,7 +73,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
7373
operation.start()
7474

7575
waitForExpectations(timeout: 1)
76-
XCTAssertTrue(operation.isFinished)
76+
waitForOperationToFinish(operation)
7777
}
7878

7979
func testvOperationGetSizeForMissingFileError() {
@@ -102,7 +102,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
102102
operation.start()
103103

104104
waitForExpectations(timeout: 1)
105-
XCTAssertTrue(operation.isFinished)
105+
waitForOperationToFinish(operation)
106106
}
107107

108108
func testUploadFileOperationUploadSuccess() {
@@ -147,7 +147,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
147147
operation.start()
148148

149149
waitForExpectations(timeout: 1)
150-
XCTAssertTrue(operation.isFinished)
150+
waitForOperationToFinish(operation)
151151
XCTAssertEqual(mockStorageService.uploadCalled, 1)
152152
mockStorageService.verifyUpload(serviceKey: expectedServiceKey,
153153
key: testKey,
@@ -194,7 +194,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
194194
operation.start()
195195

196196
waitForExpectations(timeout: 1)
197-
XCTAssertTrue(operation.isFinished)
197+
waitForOperationToFinish(operation)
198198
XCTAssertEqual(mockStorageService.uploadCalled, 1)
199199
mockStorageService.verifyUpload(serviceKey: expectedServiceKey,
200200
key: testKey,
@@ -248,7 +248,7 @@ class AWSS3StorageUploadFileOperationTests: AWSS3StorageOperationTestBase {
248248
operation.start()
249249

250250
waitForExpectations(timeout: 1)
251-
XCTAssertTrue(operation.isFinished)
251+
waitForOperationToFinish(operation)
252252
XCTAssertEqual(mockStorageService.multiPartUploadCalled, 1)
253253
mockStorageService.verifyMultiPartUpload(serviceKey: expectedServiceKey,
254254
key: testKey,

0 commit comments

Comments
 (0)