package internal import ( "io" "runtime" "testing" . "github.com/smartystreets/goconvey/convey" ) func Test_Stream(t *testing.T) { Convey("Stream", t, func() { stream := NewStream(4) // it writes burst prefill n, err := stream.Write([]byte{4, 5, 6, 7}) So(n, ShouldEqual, 4) So(err, ShouldBeNil) So(stream.burst, ShouldResemble, []byte{4, 5, 6, 7}) // it writes normally n, err = stream.Write([]byte{0, 1, 2}) So(n, ShouldEqual, 3) So(err, ShouldBeNil) So(stream.burst, ShouldResemble, []byte{7, 0, 1, 2}) // it has working subscriptions r, w := io.Pipe() stream.Subscribe(w) //So(target, ShouldHaveLength, 4) data := make([]byte, 128) n, err = r.Read(data) So(err, ShouldBeNil) So(n, ShouldEqual, 4) So(data[0:4], ShouldResemble, []byte{7, 0, 1, 2}) n, err = stream.Write([]byte{0, 0, 0, 0, 1, 0, 255, 0}) So(n, ShouldEqual, 8) So(err, ShouldBeNil) //So(target, ShouldHaveLength, 8) n, err = r.Read(data) So(err, ShouldBeNil) So(n, ShouldEqual, 8) So(data[0:8], ShouldResemble, []byte{0, 0, 0, 0, 1, 0, 255, 0}) runtime.Gosched() r.Close() n, err = r.Read(data) So(err, ShouldEqual, io.ErrClosedPipe) So(n, ShouldEqual, 0) n, err = stream.Write([]byte{8}) So(n, ShouldEqual, 1) So(err, ShouldBeNil) So(stream.SubscriberCount(), ShouldEqual, 0) }) }