| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package streamclient |
| 6 |
| 7 import ( |
| 8 "bytes" |
| 9 "errors" |
| 10 "io" |
| 11 "testing" |
| 12 |
| 13 "github.com/luci/luci-go/client/logdog/butlerlib/streamproto" |
| 14 "github.com/luci/luci-go/common/clock/clockflag" |
| 15 "github.com/luci/luci-go/common/clock/testclock" |
| 16 "github.com/luci/luci-go/common/recordio" |
| 17 . "github.com/smartystreets/goconvey/convey" |
| 18 ) |
| 19 |
| 20 type testStreamWriteCloser struct { |
| 21 bytes.Buffer |
| 22 addr string |
| 23 |
| 24 err error |
| 25 closed bool |
| 26 } |
| 27 |
| 28 func (ts *testStreamWriteCloser) Write(d []byte) (int, error) { |
| 29 if ts.err != nil { |
| 30 return 0, ts.err |
| 31 } |
| 32 return ts.Buffer.Write(d) |
| 33 } |
| 34 |
| 35 func (ts *testStreamWriteCloser) Close() error { |
| 36 ts.closed = true |
| 37 return nil |
| 38 } |
| 39 |
| 40 func TestClient(t *testing.T) { |
| 41 Convey(`A client registry with a test protocol`, t, func() { |
| 42 tswcErr := error(nil) |
| 43 tswc := (*testStreamWriteCloser)(nil) |
| 44 |
| 45 reg := protocolRegistry{} |
| 46 reg.register("test", func(addr string) (Client, error) { |
| 47 return &clientImpl{ |
| 48 factory: func() (io.WriteCloser, error) { |
| 49 tswc = &testStreamWriteCloser{ |
| 50 addr: addr, |
| 51 err: tswcErr, |
| 52 } |
| 53 return tswc, nil |
| 54 }, |
| 55 }, nil |
| 56 }) |
| 57 |
| 58 flags := streamproto.Flags{ |
| 59 Name: "test", |
| 60 Timestamp: clockflag.Time(testclock.TestTimeUTC), |
| 61 } |
| 62 |
| 63 Convey(`Will panic if the same protocol is registered twice.`, f
unc() { |
| 64 So(func() { reg.register("test", nil) }, ShouldPanic) |
| 65 So(func() { reg.register("test2", nil) }, ShouldNotPanic
) |
| 66 }) |
| 67 |
| 68 Convey(`Will fail to instantiate a Client with an invalid protoc
ol.`, func() { |
| 69 _, err := reg.newClient("fake:foo") |
| 70 So(err, ShouldNotBeNil) |
| 71 }) |
| 72 |
| 73 Convey(`Can instantiate a new client.`, func() { |
| 74 client, err := reg.newClient("test:foo") |
| 75 So(err, ShouldBeNil) |
| 76 So(client, ShouldHaveSameTypeAs, &clientImpl{}) |
| 77 |
| 78 Convey(`That can instantiate new Streams.`, func() { |
| 79 stream, err := client.NewStream(flags) |
| 80 So(err, ShouldBeNil) |
| 81 So(stream, ShouldHaveSameTypeAs, &streamImpl{}) |
| 82 |
| 83 si := stream.(*streamImpl) |
| 84 So(si.WriteCloser, ShouldHaveSameTypeAs, &testSt
reamWriteCloser{}) |
| 85 |
| 86 tswc := si.WriteCloser.(*testStreamWriteCloser) |
| 87 So(tswc.addr, ShouldEqual, "foo") |
| 88 |
| 89 Convey(`The stream should have the stream header
written to it.`, func() { |
| 90 So(tswc.Next(len(streamproto.ProtocolFra
meHeaderMagic)), ShouldResemble, |
| 91 streamproto.ProtocolFrameHeaderM
agic) |
| 92 |
| 93 r := recordio.NewReader(tswc, -1) |
| 94 f, err := r.ReadFrameAll() |
| 95 So(err, ShouldBeNil) |
| 96 So(string(f), ShouldResemble, `{"name":"
test","timestamp":"0001-02-03T04:05:06.000000007Z"}`) |
| 97 }) |
| 98 }) |
| 99 |
| 100 Convey(`If the stream fails to write the handshake, it w
ill be closed.`, func() { |
| 101 tswcErr = errors.New("test error") |
| 102 _, err := client.NewStream(flags) |
| 103 So(err, ShouldNotBeNil) |
| 104 |
| 105 So(tswc, ShouldNotBeNil) |
| 106 So(tswc.closed, ShouldBeTrue) |
| 107 }) |
| 108 }) |
| 109 }) |
| 110 } |
| OLD | NEW |