MCPcopy Create free account
hub / github.com/aceld/zinx / TestCallback

Function TestCallback

znet/callbacks_test.go:7–101  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

5)
6
7func TestCallback(t *testing.T) {
8 // Test empty list
9 cb := &callbacks{}
10 if cb.Len() != 0 {
11 t.Errorf("Expected count for empty list is 0, but got %d", cb.Len())
12 }
13
14 // Ensure invoking on an empty registry is a no-op (no panic).
15 cb.Invoke()
16
17 // Test adding callback functions
18 var count, expected, remove, totalCount int
19 totalCount = 10
20 remove = 5
21
22 // Add multiple callback functions
23 for i := 1; i < totalCount; i++ {
24 expected = expected + i
25 func(ii int) {
26 cb.Add(ii, ii, func() { count = count + ii })
27 }(i)
28 }
29
30 // Verify count after adding
31 expectedCallbacks := totalCount - 1
32 if cb.Len() != expectedCallbacks {
33 t.Errorf("Expected callback count is %d, but got %d", expectedCallbacks, cb.Len())
34 }
35
36 // Test adding nil callback
37 cb.Add(remove, remove, nil)
38 if cb.Len() != expectedCallbacks {
39 t.Errorf("Expected count after adding nil callback is %d, but got %d", expectedCallbacks, cb.Len())
40 }
41
42 // Replace an existing callback with a non-nil one; count should remain unchanged.
43 cb.Add(remove, remove, func() { count += remove })
44 if cb.Len() != expectedCallbacks {
45 t.Errorf("Expected count after replacing existing callback is %d, but got %d", expectedCallbacks, cb.Len())
46 }
47
48 // Remove specified callback
49 cb.Remove(remove, remove)
50
51 // Try to remove non-existent callback
52 cb.Remove(remove+1, remove+2)
53
54 // Execute all callbacks
55 cb.Invoke()
56
57 // Verify execution result
58 expectedSum := expected - remove
59 if count != expectedSum {
60 t.Errorf("Expected execution result is %d, but got %d", expectedSum, count)
61 }
62
63 // Test string type handler and key
64 cb2 := &callbacks{}

Callers

nothing calls this directly

Calls 5

LenMethod · 0.95
InvokeMethod · 0.95
AddMethod · 0.95
RemoveMethod · 0.95
ErrorfMethod · 0.80

Tested by

no test coverage detected