github.com/devops-filetransfer/sshego@v7.0.4+incompatible/tcplock_test.go (about) 1 package sshego 2 3 import ( 4 "fmt" 5 cv "github.com/glycerine/goconvey/convey" 6 "testing" 7 "time" 8 ) 9 10 func Test100ExclusiveTcpPortAccess(t *testing.T) { 11 12 cv.Convey("a TcpPort should be exlusive/block on Lock(), and released on Unlock()", t, func() { 13 14 var p TcpPort 15 p.Port = 65432 16 start := time.Now() 17 var unlockTm time.Time 18 p.Lock(0) 19 gotLock := make(chan time.Time) 20 go func() { 21 // B routine: 22 p.Lock(0) 23 gotLock <- time.Now() 24 }() 25 select { 26 case when := <-gotLock: 27 panic(fmt.Sprintf("problem: simultaneously 2 holders of lock after %v", when.Sub(start))) 28 case <-time.After(2000 * time.Millisecond): 29 cv.So(true, cv.ShouldEqual, true) 30 fmt.Printf("\n good: 2nd contender did not aquire lock after 2000 msec") 31 unlockTm = time.Now() 32 p.Unlock() // release goroutine 33 select { 34 case when := <-gotLock: 35 fmt.Printf("\n good: acquired lock after Unlock; took %v", when.Sub(unlockTm)) 36 cv.So(true, cv.ShouldEqual, true) 37 case <-time.After(2000 * time.Millisecond): 38 cv.So(true, cv.ShouldEqual, false) 39 fmt.Printf("\n bad: B routine did not aquire lock after 2000 msec") 40 } 41 } 42 43 }) 44 }