File: mutexkv_test.go

package info (click to toggle)
golang-github-gophercloud-utils 0.0~git20231010.80377ec-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 816 kB
  • sloc: sh: 20; makefile: 7
file content (79 lines) | stat: -rw-r--r-- 1,159 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mutexkv

import (
	"testing"
	"time"
)

func TestMutexKVLock(t *testing.T) {
	s := struct {
		mkv *MutexKV
	}{
		mkv: NewMutexKV(),
	}

	s.mkv.Lock("foo")

	doneCh := make(chan struct{})

	go func() {
		s.mkv.Lock("foo")
		close(doneCh)
	}()

	select {
	case <-doneCh:
		t.Fatal("Second lock was able to be taken. This shouldn't happen.")
	case <-time.After(50 * time.Millisecond):
		// pass
	}
}

func TestMutexKVUnlock(t *testing.T) {
	s := struct {
		mkv *MutexKV
	}{
		mkv: NewMutexKV(),
	}

	s.mkv.Lock("foo")
	s.mkv.Unlock("foo")

	doneCh := make(chan struct{})

	go func() {
		s.mkv.Lock("foo")
		close(doneCh)
	}()

	select {
	case <-doneCh:
		// pass
	case <-time.After(50 * time.Millisecond):
		t.Fatal("Second lock blocked after unlock. This shouldn't happen.")
	}
}

func TestMutexKVDifferentKeys(t *testing.T) {
	s := struct {
		mkv *MutexKV
	}{
		mkv: NewMutexKV(),
	}

	s.mkv.Lock("foo")

	doneCh := make(chan struct{})

	go func() {
		s.mkv.Lock("bar")
		close(doneCh)
	}()

	select {
	case <-doneCh:
		// pass
	case <-time.After(50 * time.Millisecond):
		t.Fatal("Second lock on a different key blocked. This shouldn't happen.")
	}
}