Debian Codegraph @

instrumentation/module_test.go

raw
1 //go:build !no_antithesis_sdk
2
3 package instrumentation
4
5 import (
6 "sync"
7 "sync/atomic"
8 "testing"
9 )
10
11 // Without a loaded library there are no leases to cache, and every hit must call
12 // in rather than being silently absorbed.
13 func TestRegisterModuleWithoutLeaseSupport(t *testing.T) {
14 module := RegisterModule("go-test.sym.tsv", 8)
15 if module.leases != nil {
16 t.Fatalf("expected no lease cache without library support, got %d words", len(module.leases))
17 }
18 // Reaching the library-less path must not panic, at any edge.
19 for _, edge := range []int{0, 1, 8, -1, 9999} {
20 module.Notify(edge)
21 }
22 }
23
24 // Each module keeps its own edge space, so concurrent traffic through two
25 // modules must not disturb the other's lease words. This exercises the per-module
26 // state that replaced the single-module globals.
27 func TestModulesDoNotShareLeaseState(t *testing.T) {
28 const edges = 4
29 currentEpoch := func() (uint64, bool) { return 2, true }
30
31 first := &Module{offset: 1 << 32, leases: make([]uint64, edges+1)}
32 second := &Module{offset: 2 << 32, leases: make([]uint64, edges+1)}
33 for edge := 1; edge <= edges; edge++ {
34 first.leases[edge] = makeLease(2, 3)
35 second.leases[edge] = makeLease(2, 3)
36 }
37
38 var start, done sync.WaitGroup
39 start.Add(1)
40 done.Add(2 * edges)
41 for edge := 1; edge <= edges; edge++ {
42 for _, module := range []*Module{first, second} {
43 go func(module *Module, edge int) {
44 defer done.Done()
45 start.Wait()
46 for i := 0; i < 3; i++ {
47 leaseLocalHit(&module.leases[edge], currentEpoch)
48 }
49 }(module, edge)
50 }
51 }
52 start.Done()
53 done.Wait()
54
55 // Every lease had exactly its grant consumed, in both modules independently.
56 for edge := 1; edge <= edges; edge++ {
57 for name, module := range map[string]*Module{"first": first, "second": second} {
58 epoch, granted, remaining := leaseFields(atomic.LoadUint64(&module.leases[edge]))
59 if epoch != 2 || granted != 3 || remaining != 0 {
60 t.Errorf("%s module edge %d: epoch=%d granted=%d remaining=%d, want 2/3/0",
61 name, edge, epoch, granted, remaining)
62 }
63 }
64 }
65 }