Streams.flatMap doesn't seem to work

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Streams.flatMap doesn't seem to work

danthegoodman
I have a service which makes between 1 and 10 HTTP calls at once, depending on the input. I want to wait for all of the promises to finish before rendering a response.

After searching the docs, I rested upon using Streams publish, flatMap and toList to merge all of the promises together. Before I dove into writing the HttpClient code, I decided to write some test code to make sure the streams api was going to do the job.

It doesn't appear to work. For some reason, the list value I am getting back is empty. After inspecting the stream a little more, it appears that flatMap doesn't forward the promised value onto the next subscriber.

Before I raise a github issue, I wanted to make sure I'm not doing anything wrong, or check if a different method exists for merging promises.

I'm using ratpack v0.9.15, and here is my test code:

@Grab('io.ratpack:ratpack-test:0.9.15')
import ratpack.test.exec.ExecHarness
import ratpack.exec.ExecResult
import ratpack.stream.Streams

def result = ExecHarness.yieldSingle {c ->
    def l = [c.promiseOf("a"), c.promiseOf("b"), c.promiseOf("c")]        
    return Streams.publish(l).flatMap({it}).toList()
}
println result.valueOrThrow
println ""

result = ExecHarness.yieldSingle {c ->
    def l = [c.promiseOf("a"), c.promiseOf("b"), c.promiseOf("c")]        
    return Streams.publish(l)
            .map { x -> println "map $x"; return x}
            .flatMap { x -> println "flat $x"; return x }
            .map { x -> println "map $x"; return "$x$x"}
            .toList()
}

println result.valueOrThrow
Reply | Threaded
Open this post in threaded view
|

Re: Streams.flatMap doesn't seem to work

Luke Daley
Administrator
I suspect that an error is being swallowed somewhere.

This:

.flatMap { x -> println "flat $x"; return x }

Should be:

.flatMap { x -> println "flat $x"; return c.promiseOf(x) }

Does that change the result?
Reply | Threaded
Open this post in threaded view
|

Re: Streams.flatMap doesn't seem to work

danthegoodman
No, the output is the same. Here is the output I get for the second block in either case:

map  [email protected]
flat [email protected]
map  [email protected]
flat [email protected]
map  [email protected]
flat [email protected]
[]
Reply | Threaded
Open this post in threaded view
|

Re: Streams.flatMap doesn't seem to work

Luke Daley
Administrator
You’re right, it’s busted.

Just patched in .16