diff options
author | Jamie Liu <jamieliu@google.com> | 2020-04-02 11:55:55 -0700 |
---|---|---|
committer | gVisor bot <gvisor-bot@google.com> | 2020-04-02 11:57:06 -0700 |
commit | 30388ff5919df33e7184719dfc6c0d9cb110b2e2 (patch) | |
tree | ad7f89a55f7af62a33c434f44a7739e0795d37d3 /pkg/sync/mutex_test.go | |
parent | 035836193e6d9e1fc9cce6a0161cb3907fbc2ef5 (diff) |
Rename files in //pkg/sync to better reflect what they contain.
PiperOrigin-RevId: 304447031
Diffstat (limited to 'pkg/sync/mutex_test.go')
-rw-r--r-- | pkg/sync/mutex_test.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/pkg/sync/mutex_test.go b/pkg/sync/mutex_test.go new file mode 100644 index 000000000..0838248b4 --- /dev/null +++ b/pkg/sync/mutex_test.go @@ -0,0 +1,71 @@ +// Copyright 2019 The gVisor Authors. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package sync + +import ( + "sync" + "testing" + "unsafe" +) + +// TestStructSize verifies that syncMutex's size hasn't drifted from the +// standard library's version. +// +// The correctness of this package relies on these remaining in sync. +func TestStructSize(t *testing.T) { + const ( + got = unsafe.Sizeof(syncMutex{}) + want = unsafe.Sizeof(sync.Mutex{}) + ) + if got != want { + t.Errorf("got sizeof(syncMutex) = %d, want = sizeof(sync.Mutex) = %d", got, want) + } +} + +// TestFieldValues verifies that the semantics of syncMutex.state from the +// standard library's implementation. +// +// The correctness of this package relies on these remaining in sync. +func TestFieldValues(t *testing.T) { + var m Mutex + m.Lock() + if got := *m.state(); got != mutexLocked { + t.Errorf("got locked sync.Mutex.state = %d, want = %d", got, mutexLocked) + } + m.Unlock() + if got := *m.state(); got != mutexUnlocked { + t.Errorf("got unlocked sync.Mutex.state = %d, want = %d", got, mutexUnlocked) + } +} + +func TestDoubleTryLock(t *testing.T) { + var m Mutex + if !m.TryLock() { + t.Fatal("failed to aquire lock") + } + if m.TryLock() { + t.Fatal("unexpectedly succeeded in aquiring locked mutex") + } +} + +func TestTryLockAfterLock(t *testing.T) { + var m Mutex + m.Lock() + if m.TryLock() { + t.Fatal("unexpectedly succeeded in aquiring locked mutex") + } +} + +func TestTryLockUnlock(t *testing.T) { + var m Mutex + if !m.TryLock() { + t.Fatal("failed to aquire lock") + } + m.Unlock() + if !m.TryLock() { + t.Fatal("failed to aquire lock after unlock") + } +} |