MCPcopy Index your code
hub / github.com/subtrace/subtrace / TestBasic

Function TestBasic

cmd/run/fd/fd_test.go:14–69  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

12)
13
14func TestBasic(t *testing.T) {
15 b := make([]byte, 10000)
16 if _, err := rand.Read(b[:]); err != nil {
17 panic(err)
18 }
19
20 fd := NewFD(1234)
21 fd.DecRef()
22
23 var wg sync.WaitGroup
24
25 var entries atomic.Uint64
26 var done atomic.Bool
27 final := -1
28 wg.Add(1)
29 go func() {
30 defer wg.Done()
31 defer t.Logf("finished close")
32 time.Sleep(30 * time.Millisecond)
33 if !fd.IncRefLockForClose() {
34 t.Fatalf("failed to incref for close")
35 }
36 defer fd.DecRef()
37 final = int(entries.Load())
38 done.Store(true)
39 t.Logf("closing (entered=%d)", final)
40 }()
41
42 rounds := 0
43 for !done.Load() {
44 rounds++
45 for i := 0; i < len(b); i++ {
46 wg.Add(1)
47 go func(i int) {
48 defer wg.Done()
49 if err := fd.IncRef(); err != nil {
50 return
51 }
52 entries.Add(1)
53 defer fd.DecRef()
54 dur := time.Duration(int(b[i])) * time.Microsecond
55 time.Sleep(dur)
56 if got := fd.FD(); got != 1234 {
57 t.Fatalf("failed: got %d, want 1234", got)
58 }
59 }(i)
60 }
61 }
62
63 wg.Wait()
64
65 if got := int(entries.Load()); got != final {
66 t.Fatalf("got %d, want %d", got, final)
67 }
68 t.Logf("final: %d/%d entries", entries.Load(), rounds*len(b))
69}

Callers

nothing calls this directly

Calls 9

DecRefMethod · 0.95
IncRefMethod · 0.95
FDMethod · 0.95
NewFDFunction · 0.85
AddMethod · 0.80
LoadMethod · 0.80
DoneMethod · 0.65
ReadMethod · 0.45
WaitMethod · 0.45

Tested by

no test coverage detected