go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butler/output/memory/memory_test.go (about) 1 // Copyright 2021 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package memory 16 17 import ( 18 "context" 19 "fmt" 20 "testing" 21 22 . "github.com/smartystreets/goconvey/convey" 23 24 "go.chromium.org/luci/logdog/api/logpb" 25 "go.chromium.org/luci/logdog/client/butler" 26 "go.chromium.org/luci/logdog/client/butlerlib/streamclient" 27 ) 28 29 func TestMemory(t *testing.T) { 30 Convey(`Test Memory Output`, t, func() { 31 ctx, cancel := context.WithCancel(context.Background()) 32 defer cancel() 33 34 m := &Output{} 35 defer m.Close() // noop 36 37 butler, err := butler.New(ctx, butler.Config{Output: m}) 38 So(err, ShouldBeNil) 39 40 didStop := false 41 butlerStop := func() { 42 if !didStop { 43 didStop = true 44 butler.Activate() 45 So(butler.Wait(), ShouldBeNil) 46 } 47 } 48 defer butlerStop() 49 50 sc := streamclient.NewLoopback(butler, "ns") 51 52 Convey(`text`, func() { 53 stream, err := sc.NewStream(ctx, "hello", streamclient.WithTags("neat", "thingy")) 54 So(err, ShouldBeNil) 55 56 _, err = fmt.Fprintln(stream, "hello world!") 57 So(err, ShouldBeNil) 58 _, err = fmt.Fprintln(stream, "this is pretty cool.") 59 So(err, ShouldBeNil) 60 61 So(stream.Close(), ShouldBeNil) 62 butlerStop() 63 64 outStream := m.GetStream("", "ns/hello") 65 So(outStream, ShouldNotBeNil) 66 67 So(outStream.Tags(), ShouldResemble, map[string]string{"neat": "thingy"}) 68 So(outStream.LastData(), ShouldEqual, "hello world!\nthis is pretty cool.\n") 69 So(outStream.StreamType(), ShouldEqual, logpb.StreamType_TEXT) 70 }) 71 72 Convey(`binary`, func() { 73 stream, err := sc.NewStream(ctx, "hello", streamclient.WithTags("neat", "thingy"), streamclient.Binary()) 74 So(err, ShouldBeNil) 75 76 _, err = fmt.Fprintln(stream, "hello world!") 77 So(err, ShouldBeNil) 78 _, err = fmt.Fprintln(stream, "this is pretty cool.") 79 So(err, ShouldBeNil) 80 81 So(stream.Close(), ShouldBeNil) 82 butlerStop() 83 84 outStream := m.GetStream("", "ns/hello") 85 So(outStream, ShouldNotBeNil) 86 87 So(outStream.Tags(), ShouldResemble, map[string]string{"neat": "thingy"}) 88 So(outStream.LastData(), ShouldEqual, "hello world!\nthis is pretty cool.\n") 89 So(outStream.StreamType(), ShouldEqual, logpb.StreamType_BINARY) 90 }) 91 92 Convey(`datagram`, func() { 93 stream, err := sc.NewDatagramStream(ctx, "hello", streamclient.WithTags("neat", "thingy")) 94 So(err, ShouldBeNil) 95 96 So(stream.WriteDatagram([]byte("hello world")), ShouldBeNil) 97 So(stream.WriteDatagram([]byte("this is pretty cool")), ShouldBeNil) 98 99 So(stream.Close(), ShouldBeNil) 100 butlerStop() 101 102 outStream := m.GetStream("", "ns/hello") 103 So(outStream, ShouldNotBeNil) 104 105 So(outStream.Tags(), ShouldResemble, map[string]string{"neat": "thingy"}) 106 So(outStream.LastData(), ShouldEqual, "this is pretty cool") 107 So(outStream.AllData(), ShouldResemble, []string{ 108 "hello world", 109 "this is pretty cool", 110 }) 111 So(outStream.StreamType(), ShouldEqual, logpb.StreamType_DATAGRAM) 112 }) 113 114 }) 115 }