go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/auth/integration/internal/localsrv/localsrv_test.go (about) 1 // Copyright 2017 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 localsrv 16 17 import ( 18 "context" 19 "net" 20 "sync" 21 "testing" 22 23 . "github.com/smartystreets/goconvey/convey" 24 . "go.chromium.org/luci/common/testing/assertions" 25 ) 26 27 func noopServe(context.Context, net.Listener, *sync.WaitGroup) error { 28 return nil 29 } 30 31 func TestServerLifecycle(t *testing.T) { 32 t.Parallel() 33 34 ctx := context.Background() 35 36 Convey("Double Start", t, func() { 37 s := Server{} 38 defer s.Stop(ctx) 39 _, err := s.Start(ctx, "test", 0, noopServe) 40 So(err, ShouldBeNil) 41 _, err = s.Start(ctx, "test", 0, noopServe) 42 So(err, ShouldErrLike, "already initialized") 43 }) 44 45 Convey("Start after Stop", t, func() { 46 s := Server{} 47 _, err := s.Start(ctx, "test", 0, noopServe) 48 So(err, ShouldBeNil) 49 So(s.Stop(ctx), ShouldBeNil) 50 _, err = s.Start(ctx, "test", 0, noopServe) 51 So(err, ShouldErrLike, "already initialized") 52 }) 53 54 Convey("Stop works", t, func() { 55 serving := make(chan struct{}) 56 s := Server{} 57 _, err := s.Start(ctx, "test", 0, func(c context.Context, l net.Listener, wg *sync.WaitGroup) error { 58 close(serving) 59 <-c.Done() // the context is canceled by Stop() call below 60 return nil 61 }) 62 So(err, ShouldBeNil) 63 64 <-serving // wait until really started 65 66 // Stop it. 67 So(s.Stop(ctx), ShouldBeNil) 68 // Doing it second time is ok too. 69 So(s.Stop(ctx), ShouldBeNil) 70 }) 71 }