package sluice

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func Test(t *testing.T) {
	Convey("Sluice can...", t, func() {
		gondola := New()

		Convey("pump values", func() {
			gondola.Push("x")
			gondola.Push("y")
			gondola.Push("z")
			So(<-gondola.Next(), ShouldEqual, "x")
			So(<-gondola.Next(), ShouldEqual, "y")
			So(<-gondola.Next(), ShouldEqual, "z")
		})

		Convey("block when empty", func() {
			var answered bool
			select {
			case <-gondola.Next():
				answered = true
			default:
				answered = false
			}
			So(answered, ShouldEqual, false)

			Convey("answers even dropped channels", func() {
				secondReq := gondola.Next()
				gondola.Push("1")
				// we still don't expect an answer,
				//  because the "1" routed to the channel in the prev test.
				select {
				case <-secondReq:
					answered = true
				default:
					answered = false
				}
				So(answered, ShouldEqual, false)

				Convey("definitely answers eventually", func() {
					gondola.Push("2")
					So(<-secondReq, ShouldEqual, "2")
				})
			})
		})
	})
}

package sluice

import (
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func Test(t *testing.T) {
	Convey("Sluice can...", t, func() {
		gondola := New()

		Convey("pump values", func() {
			gondola.Push("x")
			gondola.Push("y")
			gondola.Push("z")
			So(<-gondola.Next(), ShouldEqual, "x")
			So(<-gondola.Next(), ShouldEqual, "y")
			So(<-gondola.Next(), ShouldEqual, "z")
		})

		Convey("block when empty", func() {
			var answered bool
			select {
			case <-gondola.Next():
				answered = true
			default:
				answered = false
			}
			So(answered, ShouldEqual, false)

			Convey("answers even dropped channels", func() {
				gondola.Push("1")
				// we still don't expect an answer,
				//  because the "1" routed to the channel in the prev test.
				secondReq := gondola.Next()
				select {
				case <-secondReq:
					answered = true
				default:
					answered = false
				}
				So(answered, ShouldEqual, false)

				Convey("definitely answers eventually", func() {
					gondola.Push("2")
					So(<-secondReq, ShouldEqual, "2")
				})
			})
		})
	})
}

Fix a sluice test with probablistic failure.
Fix a sluice test with probablistic failure.

The synchronization wasn't wrong: the test was.  If you sign up another channel first, *then* send, it's random -- up to map order -- which one will get it.

The test now sends that message, then signs up the next channel, making the behavior predictable.

Though it's always amusing to me to see things like map iteration on a 2-item map be "AB" 80% of the time and "BA" 20% of the time.  An interesting form of random, that.  Nobody promised uniform distribution, just... wat.

Signed-off-by: Eric Myhre <2346ad27d7568ba9896f1b7da6b5991251debdf2@exultant.us>
