Skip to content

Commit

Permalink
Fix bug in the Swift Ice/ami test
Browse files Browse the repository at this point in the history
  • Loading branch information
externl committed Oct 31, 2024
1 parent 0e1f210 commit 28091b1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
3 changes: 0 additions & 3 deletions swift/test/Ice/ami/AllTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import Foundation
import Ice
import TestCommon

// WORKAROUND: Disable optimization for this test. Otherwise we get a crash in
// "testing batch requests with communicator"
@_optimize(none)
func allTests(_ helper: TestHelper, collocated: Bool = false) async throws {
func test(_ value: Bool, file: String = #file, line: Int = #line) throws {
try helper.test(value, file: file, line: line)
Expand Down
22 changes: 13 additions & 9 deletions swift/test/Ice/ami/TestI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,26 @@ class TestI: TestIntf {
}

func waitForBatch(count: Int32, current _: Current) async throws -> Bool {
if await count == _state.getBatchCount() {
await _state.clearBatchCount()
return true
}

var sub: AnyCancellable?
let currentCount = await withCheckedContinuation { continuation in
await withCheckedContinuation { continuation in
sub = _batchSubject.sink { batchCount in
if batchCount >= count {
continuation.resume(returning: batchCount)
if batchCount == count {
continuation.resume()
} else if batchCount > count {
fatalError("Received more batches than expected: \(batchCount) > \(count)")
}
}
}
sub!.cancel()
let result = count == currentCount
// Check the batch count again after the sink has been cancelled to get the current value.
// The client only ever sends the expect number of batches so it is impossible that the
// batch count will be greater than the expected count at this point unless there is a bug.
// The above sink with also crash if the continuation is resumed twice if an unexpected batch is
// received before the subscription is cancelled.
let batchCount = await _state.getBatchCount()
let result = count == batchCount
await _state.clearBatchCount()
_batchSubject.send(0) // Reset batch subject
return result
}

Expand Down

0 comments on commit 28091b1

Please sign in to comment.