Combine Publishers.CombineLatest has a serious demand management bug - I believe.

Hello Eskimo, check the example below...

If the documentation for Publishers.Combine would be correct, it would always announce an unlimited demand to the upstream publishers - and it doesn't. There also seems to be a bug if downstream requests a demand of .max(1).

Somehow I am pretty puzzled by this bug... seems there is just limited unit testing for an API? Or is there something I don't understand?

I have filed a radar: FB10446601 - wondering if anyone will look at it...

Testcase output attached.

import Foundation
import Combine

import XCTest

class TestCombineLatest:XCTestCase {
    func testCombineLatest() {
        let latest1 = PassthroughSubject<Int,Never>()
        let latest2 = PassthroughSubject<Int,Never>()
        
        
        var result:[[Int]] = []
        var subscription:Subscription?
        
        let subscriber = AnySubscriber<(Int,Int),Never>(
            receiveSubscription: {sub in
                subscription = sub
                sub.request(.max(1))
                // replace with sup.request(.unlimited) and the test case succeeds.
            },
            receiveValue: { (v1,v2) in
                result.append([v1,v2])
                return .max(1)
            },
            receiveCompletion: {_ in}
        )
        
        let publisher = Publishers.CombineLatest(latest1.print("Latest1"), latest2.print("Latest2"))
            .print("CombineLatest")
        
        publisher
            .subscribe(subscriber)
        
        latest1.send(1)
        latest2.send(1)
        latest1.send(2) //<- has no effect...
        latest2.send(2)
        
        latest1.send(completion: .finished)
        latest2.send(completion: .finished)
        
        print("Result is:\(result)")
        
        XCTAssertEqual(result, [[1,1], [2,1], [2,2] ])
    
    }}

Combine Publishers.CombineLatest has a serious demand management bug - I believe.
 
 
Q