| 57 | } |
| 58 | |
| 59 | func (s *FileLockSuite) TestSharedSimple(c *C) { |
| 60 | lockName := "testflock1" |
| 61 | |
| 62 | fl1 := New(lockName) |
| 63 | fl2 := New(lockName) |
| 64 | fl3 := New(lockName) |
| 65 | |
| 66 | // test we can acquire multiple shared locks |
| 67 | c.Assert(fl1.RLock(), IsNil) |
| 68 | c.Assert(fl2.TryRLock(), IsNil) |
| 69 | c.Assert(fl3.TryLock(), NotNil) |
| 70 | |
| 71 | // unlock one shared lock, exclusive lock should fail |
| 72 | c.Assert(fl1.RUnlock(), IsNil) |
| 73 | c.Assert(fl3.TryLock(), NotNil) |
| 74 | |
| 75 | // unlock both shared locks, exclusive lock should succeed |
| 76 | c.Assert(fl2.RUnlock(), IsNil) |
| 77 | c.Assert(fl3.TryLock(), IsNil) |
| 78 | |
| 79 | // now try to acquire blocking shared locks in Go Routine |
| 80 | doneChan := make(chan struct{}, 0) |
| 81 | go func() { |
| 82 | c.Assert(fl1.RLock(), IsNil) |
| 83 | c.Assert(fl2.RLock(), IsNil) |
| 84 | close(doneChan) |
| 85 | }() |
| 86 | |
| 87 | select { |
| 88 | case <-doneChan: |
| 89 | c.Fatal("Locking block didn't actually block!") |
| 90 | case <-time.After(10 * time.Millisecond): |
| 91 | } |
| 92 | |
| 93 | // shared locks should succeed as soon as we Unlock |
| 94 | c.Assert(fl3.Unlock(), IsNil) |
| 95 | |
| 96 | <-doneChan |
| 97 | |
| 98 | c.Assert(fl1.RUnlock(), IsNil) |
| 99 | |
| 100 | // lock exclusive should not succeed until all RUnlocked |
| 101 | doneChan = make(chan struct{}, 0) |
| 102 | go func() { |
| 103 | c.Assert(fl3.Lock(), IsNil) |
| 104 | close(doneChan) |
| 105 | }() |
| 106 | |
| 107 | select { |
| 108 | case <-doneChan: |
| 109 | c.Fatal("Locking block didn't actually block!") |
| 110 | case <-time.After(10 * time.Millisecond): |
| 111 | } |
| 112 | |
| 113 | c.Assert(fl2.RUnlock(), IsNil) |
| 114 | <-doneChan |
| 115 | c.Assert(fl3.Unlock(), IsNil) |
| 116 | |