forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserinfo_cache_test.go
More file actions
50 lines (44 loc) · 1.71 KB
/
Copy pathuserinfo_cache_test.go
File metadata and controls
50 lines (44 loc) · 1.71 KB
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
package main
import (
"fmt"
"sync"
"testing"
)
// TestUpdateUserInfoCopyOnWrite proves updateUserInfo does not mutate the
// shared map. The map inside Values lives in userinfocache and is handed to
// request goroutines via the request context, so an in-place write races with
// concurrent readers. This assertion is deterministic (no -race needed): the
// previous implementation mutated `base` and would fail here.
func TestUpdateUserInfoCopyOnWrite(t *testing.T) {
base := Values{m: map[string]string{"Events": "Message", "Webhook": "https://example.test"}}
updated := updateUserInfo(base, "Events", "Message,ReadReceipt").(Values)
if got := base.Get("Events"); got != "Message" {
t.Errorf("shared Values was mutated in place: base Events=%q, want %q", got, "Message")
}
if got := updated.Get("Events"); got != "Message,ReadReceipt" {
t.Errorf("returned Values not updated: Events=%q, want %q", got, "Message,ReadReceipt")
}
if got := updated.Get("Webhook"); got != "https://example.test" {
t.Errorf("returned Values lost an unrelated field: Webhook=%q", got)
}
}
// TestUpdateUserInfoConcurrent exercises the data race directly. Run with
// `go test -race`: the previous in-place mutation triggered "concurrent map
// read and map write" when one goroutine updated while another read. The
// copy-on-write version never writes to the shared map, so it is clean.
func TestUpdateUserInfoConcurrent(t *testing.T) {
base := Values{m: map[string]string{"Events": "Message"}}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(2)
go func(i int) {
defer wg.Done()
_ = updateUserInfo(base, "Events", fmt.Sprintf("v%d", i))
}(i)
go func() {
defer wg.Done()
_ = base.Get("Events")
}()
}
wg.Wait()
}