File: thread_safe_map_test.go

package info (click to toggle)
lazygit 0.50.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,808 kB
  • sloc: sh: 128; makefile: 76
file content (59 lines) | stat: -rw-r--r-- 866 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
package utils

import (
	"testing"
)

func TestThreadSafeMap(t *testing.T) {
	m := NewThreadSafeMap[int, int]()

	m.Set(1, 1)
	m.Set(2, 2)
	m.Set(3, 3)

	if m.Len() != 3 {
		t.Errorf("Expected length to be 3, got %d", m.Len())
	}

	if !m.Has(1) {
		t.Errorf("Expected to have key 1")
	}

	if m.Has(4) {
		t.Errorf("Expected to not have key 4")
	}

	if _, ok := m.Get(1); !ok {
		t.Errorf("Expected to have key 1")
	}

	if _, ok := m.Get(4); ok {
		t.Errorf("Expected to not have key 4")
	}

	m.Delete(1)

	if m.Has(1) {
		t.Errorf("Expected to not have key 1")
	}

	m.Clear()

	if m.Len() != 0 {
		t.Errorf("Expected length to be 0, got %d", m.Len())
	}
}

func TestThreadSafeMapConcurrentReadWrite(t *testing.T) {
	m := NewThreadSafeMap[int, int]()

	go func() {
		for i := 0; i < 10000; i++ {
			m.Set(0, 0)
		}
	}()

	for i := 0; i < 10000; i++ {
		m.Get(0)
	}
}