@@ -7,31 +7,33 @@ package diskcache
77
88import (
99 "os"
10- "runtime "
10+ "path/filepath "
1111 "sync"
1212 T "testing"
1313 "time"
1414
1515 "github.com/stretchr/testify/assert"
1616)
1717
18- func TestPidAlive (t * T.T ) {
19- t .Run ("pid-1" , func (t * T.T ) {
20- if runtime .GOOS != "windows" {
21- assert .True (t , pidAlive (1 ))
22- }
23- })
18+ func TestLockUnlock (t * T.T ) {
19+ t .Run ("unlock-remove" , func (t * T.T ) {
20+ p := t .TempDir ()
21+ fl := newFlock (p )
2422
25- t .Run ("pid-not-exist" , func (t * T.T ) {
26- assert .False (t , pidAlive (- 1 ))
27- })
23+ ok , err := fl .tryLock ()
24+ assert .True (t , ok )
25+ assert .NoError (t , err )
26+
27+ fi , err := os .Stat (filepath .Join (p , ".lock" ))
28+ assert .NoError (t , err )
29+ t .Logf ("fi: %+#v" , fi )
2830
29- t .Run ("cur-pid" , func (t * T.T ) {
30- assert .True (t , pidAlive (os .Getpid ()))
31+ fl .unlock ()
32+
33+ _ , err = os .Stat (filepath .Join (p , ".lock" ))
34+ assert .Error (t , err )
3135 })
32- }
3336
34- func TestLockUnlock (t * T.T ) {
3537 t .Run ("lock" , func (t * T.T ) {
3638 p := t .TempDir ()
3739
@@ -42,7 +44,10 @@ func TestLockUnlock(t *T.T) {
4244 defer wg .Done ()
4345 fl := newFlock (p )
4446
45- assert .NoError (t , fl .lock ())
47+ ok , err := fl .tryLock ()
48+
49+ assert .True (t , ok )
50+ assert .NoError (t , err )
4651 defer fl .unlock ()
4752
4853 time .Sleep (time .Second * 5 )
@@ -54,7 +59,8 @@ func TestLockUnlock(t *T.T) {
5459 defer wg .Done ()
5560 fl := newFlock (p )
5661
57- err := fl .lock ()
62+ ok , err := fl .tryLock ()
63+ assert .False (t , ok )
5864 assert .Error (t , err )
5965
6066 t .Logf ("[expect] err: %s" , err .Error ())
@@ -68,7 +74,7 @@ func TestLockUnlock(t *T.T) {
6874
6975 // try lock until ok
7076 for {
71- if err := fl .lock (); err != nil {
77+ if ok , err := fl .tryLock (); ! ok {
7278 t .Logf ("[expect] err: %s" , err .Error ())
7379 time .Sleep (time .Second )
7480 } else {
0 commit comments