go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/logdog/client/butlerlib/streamclient/protocol_null_test.go (about)

     1  // Copyright 2019 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 streamclient
    16  
    17  import (
    18  	"context"
    19  	"os"
    20  	"testing"
    21  
    22  	"go.chromium.org/luci/common/clock/testclock"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  	. "go.chromium.org/luci/common/testing/assertions"
    26  )
    27  
    28  func TestNullProtocol(t *testing.T) {
    29  	Convey(`"null" protocol Client`, t, func() {
    30  		ctx, _ := testclock.UseTime(context.Background(), testclock.TestTimeUTC)
    31  
    32  		Convey(`good`, func() {
    33  			client, err := New("null", "namespace")
    34  			So(err, ShouldBeNil)
    35  
    36  			Convey(`can use a text stream`, func() {
    37  				stream, err := client.NewStream(ctx, "test")
    38  				So(err, ShouldBeNil)
    39  
    40  				n, err := stream.Write([]byte("hi"))
    41  				So(n, ShouldEqual, 2)
    42  				So(err, ShouldBeNil)
    43  				So(stream.Close(), ShouldBeNil)
    44  			})
    45  
    46  			Convey(`can use a text stream for a subprocess`, func() {
    47  				stream, err := client.NewStream(ctx, "test", ForProcess())
    48  				So(err, ShouldBeNil)
    49  				defer stream.Close()
    50  				So(stream, ShouldHaveSameTypeAs, (*os.File)(nil))
    51  
    52  				n, err := stream.Write([]byte("hi"))
    53  				So(n, ShouldEqual, 2)
    54  				So(err, ShouldBeNil)
    55  				So(stream.Close(), ShouldBeNil)
    56  				So(stream.Close(), ShouldErrLike, os.ErrClosed)
    57  			})
    58  
    59  			Convey(`can use a binary stream`, func() {
    60  				stream, err := client.NewStream(ctx, "test", Binary())
    61  				So(err, ShouldBeNil)
    62  
    63  				n, err := stream.Write([]byte{0, 1, 2, 3})
    64  				So(n, ShouldEqual, 4)
    65  				So(err, ShouldBeNil)
    66  				So(stream.Close(), ShouldBeNil)
    67  			})
    68  
    69  			Convey(`can use a datagram stream`, func() {
    70  				stream, err := client.NewDatagramStream(ctx, "test")
    71  				So(err, ShouldBeNil)
    72  
    73  				So(stream.WriteDatagram([]byte("hi")), ShouldBeNil)
    74  				So(stream.WriteDatagram([]byte("there")), ShouldBeNil)
    75  				So(stream.Close(), ShouldBeNil)
    76  			})
    77  		})
    78  	})
    79  }