github.com/ethersphere/bee/v2@v2.2.0/pkg/spinlock/wait_test.go (about) 1 // Copyright 2022 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package spinlock_test 6 7 import ( 8 "errors" 9 "testing" 10 "time" 11 12 "github.com/ethersphere/bee/v2/pkg/spinlock" 13 ) 14 15 func TestWait(t *testing.T) { 16 t.Parallel() 17 18 t.Run("timed out", func(t *testing.T) { 19 t.Parallel() 20 21 err := spinlock.Wait(time.Millisecond*20, func() bool { return false }) 22 if !errors.Is(err, spinlock.ErrTimedOut) { 23 t.Fatal("expecting to time out") 24 } 25 }) 26 27 t.Run("condition satisfied", func(t *testing.T) { 28 t.Parallel() 29 30 spinStartTime := time.Now() 31 condCallCount := 0 32 err := spinlock.Wait(time.Millisecond*200, func() bool { 33 condCallCount++ 34 return time.Since(spinStartTime) >= time.Millisecond*100 35 }) 36 if err != nil { 37 t.Fatal("expecting to end wait without time out") 38 } 39 if condCallCount == 0 { 40 t.Fatal("expecting condition function to be called") 41 } 42 }) 43 }