# Awaiting fulfillment(of:timeout:enforceOrder:) doesn’t wait?

**URL:** <https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386>\
**Category:** Using Swift\
**Tags:** xctest, structured-concurren\
**Created:** [January 11, 2024, 7:57am UTC](https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386 "2024-01-11T07:57:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [January 11, 2024, 7:57am UTC](https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386/1 "2024-01-11T07:57:16Z")

</div>

I'm trying to unit test some code that has a series of async operations (StoreKit code). I have to sink on a @Publisher and wait to get the right value, then call a method, and do it again. But it seems awaiting fulfillment doesn’t actually block execution:

In the following code, there are two `await fulfillment(of:)` calls. I had expected the first one to wait until it was fulfilled, or until it timed out. But it does not, and the code immediately proceeds to delete the transaction it had created a bit earlier.

This is very counterintuitive to me. I can't imagine it's considered to be a bug, so is there another way for me to essentially stop the progress of a unit test until an `@Published` property has a particular value?

```swift
let session = try SKTestSession(configurationFileNamed: "Re-Key")
session.clearTransactions()

let purchaseExpectation = XCTestExpectation(description: "purchaseExpectation")

let store = Store.shared
_ = store.$expires.sink
{ inValue in
	if let hasSub = store.hasSubscription,
		hasSub
	{
		purchaseExpectation.fulfill()
	}
}

let txn = try await session.buyProduct(identifier: "rekey.lifetime")
await fulfillment(of: [purchaseExpectation], timeout: 5.0)

//	Delete the purchase…

let deleteExpectation = XCTestExpectation(description: "deleteExpectation")
_ = store.$expires.sink
{ inValue in
	if store.hasSubscription == nil
	{
		deleteExpectation.fulfill()
	}
}

try session.deleteTransaction(identifier: UInt(txn.id))
await fulfillment(of: [deleteExpectation], timeout: 5.0)

```

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [January 11, 2024, 2:12pm UTC](https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386/2 "2024-01-11T14:12:10Z")

</div>

You're saying `purchaseExpectation` isn't fulfilled by the time `await fulfillment` completes? If so, that definitely a bug, given its purpose. But fulfillment of that expectation is all that statement waits for.

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [January 12, 2024, 12:00am UTC](https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386/3 "2024-01-12T00:00:33Z")

</div>

Sure seems like what I'm seeing. Let me see if I can't make a self-contained example.

---

<div class="post-metadata">

**Author:** ![gestrich](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gestrich/32/18000_2.png) [@gestrich](https://forums.swift.org/u/gestrich)\
**Post date:** [January 12, 2024, 11:15am UTC](https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386/4 "2024-01-12T11:15:34Z")

</div>

I'm not sure if this is the same issue but I recall being surprised in unit tests when sink would be called for my initial value, not what I was changing in my unit test. You may want to check [swift - Prevent sink receiveValue closure from being called immediately - Stack Overflow](https://stackoverflow.com/questions/60568858/prevent-sink-receivevalue-closure-from-being-called-immediately)

---

<div class="post-metadata">

**Author:** ![JetForMe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jetforme/32/482_2.png) [@JetForMe](https://forums.swift.org/u/JetForMe)\
**Post date:** [January 12, 2024, 4:00pm UTC](https://forums.swift.org/t/awaiting-fulfillment-ofenforceorder-doesn-t-wait/69386/5 "2024-01-12T16:00:34Z")

</div>

I did have this issue but I had addressed it. But perhaps it confounded my current problem because it does seem to be behaving properly now.
