github.com/ks888/tgo@v0.0.0-20190130135156-80bf89407292/tracer/breakpoints_test.go (about) 1 package tracer 2 3 import "testing" 4 5 func TestBreakpoints_SetHitAndClear(t *testing.T) { 6 numSet, numCleared := 0, 0 7 setBreakpoint := func(uint64) error { numSet++; return nil } 8 clearBreakpoint := func(uint64) error { numCleared++; return nil } 9 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 10 11 if err := bps.Set(0x100); err != nil { 12 t.Fatalf("failed to set breakpoint: %v", err) 13 } 14 15 if !bps.Hit(0x100, 1) { 16 t.Errorf("not hit") 17 } 18 19 if err := bps.Clear(0x100); err != nil { 20 t.Fatalf("failed to clear breakpoint: %v", err) 21 } 22 23 if numSet != 1 { 24 t.Errorf("wrong number of set ops: %d", numSet) 25 } 26 if numCleared != 1 { 27 t.Errorf("wrong number of clear ops: %d", numCleared) 28 } 29 } 30 31 func TestBreakpoints_SetHitAndClearConditional(t *testing.T) { 32 numSet, numCleared := 0, 0 33 setBreakpoint := func(uint64) error { numSet++; return nil } 34 clearBreakpoint := func(uint64) error { numCleared++; return nil } 35 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 36 37 if err := bps.SetConditional(0x100, 1); err != nil { 38 t.Fatalf("failed to set breakpoint: %v", err) 39 } 40 41 if !bps.Hit(0x100, 1) { 42 t.Errorf("not hit") 43 } 44 45 if err := bps.ClearConditional(0x100, 1); err != nil { 46 t.Fatalf("failed to clear breakpoint: %v", err) 47 } 48 49 if numSet != 1 { 50 t.Errorf("wrong number of set ops: %d", numSet) 51 } 52 if numCleared != 1 { 53 t.Errorf("wrong number of clear ops: %d", numCleared) 54 } 55 } 56 57 func TestBreakpoints_Set_SetConditionalBefore(t *testing.T) { 58 setBreakpoint := func(uint64) error { return nil } 59 clearBreakpoint := func(uint64) error { return nil } 60 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 61 62 if err := bps.SetConditional(0x100, 1); err != nil { 63 t.Fatalf("failed to set breakpoint: %v", err) 64 } 65 66 if err := bps.Set(0x100); err != nil { 67 t.Fatalf("failed to set breakpoint: %v", err) 68 } 69 70 if !bps.Hit(0x100, 2) { 71 t.Errorf("previous conditions are not removed") 72 } 73 } 74 75 func TestBreakpoints_SetConditional_SetBefore(t *testing.T) { 76 setBreakpoint := func(uint64) error { return nil } 77 clearBreakpoint := func(uint64) error { return nil } 78 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 79 80 if err := bps.Set(0x100); err != nil { 81 t.Fatalf("failed to set breakpoint: %v", err) 82 } 83 84 if err := bps.SetConditional(0x100, 1); err != nil { 85 t.Fatalf("failed to set breakpoint: %v", err) 86 } 87 88 if !bps.Hit(0x100, 2) { 89 t.Errorf("SetConditional should be no-op here") 90 } 91 } 92 93 func TestBreakpoints_Hit_NotSet(t *testing.T) { 94 setBreakpoint := func(uint64) error { return nil } 95 clearBreakpoint := func(uint64) error { return nil } 96 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 97 98 if bps.Hit(0x100, 1) { 99 t.Errorf("should not hit") 100 } 101 } 102 103 func TestBreakpoints_Hit_NotMeetCondition(t *testing.T) { 104 setBreakpoint := func(uint64) error { return nil } 105 clearBreakpoint := func(uint64) error { return nil } 106 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 107 108 if err := bps.SetConditional(0x100, 1); err != nil { 109 t.Fatalf("failed to set breakpoint: %v", err) 110 } 111 112 if bps.Hit(0x100, 2) { 113 t.Errorf("should not hit") 114 } 115 } 116 117 func TestBreakpoints_Clear_ClearConditionals(t *testing.T) { 118 setBreakpoint := func(uint64) error { return nil } 119 clearBreakpoint := func(uint64) error { return nil } 120 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 121 122 if err := bps.SetConditional(0x100, 1); err != nil { 123 t.Fatalf("failed to set breakpoint: %v", err) 124 } 125 126 if err := bps.Clear(0x100); err != nil { 127 t.Fatalf("failed to clear breakpoint: %v", err) 128 } 129 130 if bps.Hit(0x100, 1) { 131 t.Errorf("should not hit") 132 } 133 } 134 135 func TestBreakpoints_ClearConditional_OtherCondtionsRemain(t *testing.T) { 136 numCleared := 0 137 setBreakpoint := func(uint64) error { return nil } 138 clearBreakpoint := func(uint64) error { numCleared++; return nil } 139 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 140 141 if err := bps.SetConditional(0x100, 1); err != nil { 142 t.Fatalf("failed to set breakpoint: %v", err) 143 } 144 145 if err := bps.SetConditional(0x100, 2); err != nil { 146 t.Fatalf("failed to set breakpoint: %v", err) 147 } 148 149 if err := bps.ClearConditional(0x100, 1); err != nil { 150 t.Fatalf("failed to clear breakpoint: %v", err) 151 } 152 153 if numCleared != 0 { 154 t.Errorf("wrong number of clear ops: %d", numCleared) 155 } 156 } 157 158 func TestBreakpoints_ClearAllByGoRoutineID(t *testing.T) { 159 numCleared := 0 160 setBreakpoint := func(uint64) error { return nil } 161 clearBreakpoint := func(uint64) error { numCleared++; return nil } 162 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 163 164 if err := bps.SetConditional(0x100, 1); err != nil { 165 t.Fatalf("failed to set breakpoint: %v", err) 166 } 167 168 if err := bps.SetConditional(0x200, 1); err != nil { 169 t.Fatalf("failed to set breakpoint: %v", err) 170 } 171 172 if err := bps.ClearAllByGoRoutineID(1); err != nil { 173 t.Fatalf("failed to clear breakpoint: %v", err) 174 } 175 176 if numCleared != 2 { 177 t.Errorf("wrong number of clear ops: %d", numCleared) 178 } 179 } 180 181 func TestBreakpoints_ClearAllByGoRoutineID_DuplicateBreakpoints(t *testing.T) { 182 numCleared := 0 183 setBreakpoint := func(uint64) error { return nil } 184 clearBreakpoint := func(uint64) error { numCleared++; return nil } 185 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 186 187 if err := bps.SetConditional(0x100, 1); err != nil { 188 t.Fatalf("failed to set breakpoint: %v", err) 189 } 190 191 if err := bps.SetConditional(0x100, 1); err != nil { 192 t.Fatalf("failed to set breakpoint: %v", err) 193 } 194 195 if err := bps.ClearAllByGoRoutineID(1); err != nil { 196 t.Fatalf("failed to clear breakpoint: %v", err) 197 } 198 199 if numCleared != 1 { 200 t.Errorf("wrong number of clear ops: %d", numCleared) 201 } 202 } 203 204 func TestBreakpoints_ClearAllByGoRoutineID_NotConditionalBP(t *testing.T) { 205 numCleared := 0 206 setBreakpoint := func(uint64) error { return nil } 207 clearBreakpoint := func(uint64) error { numCleared++; return nil } 208 bps := NewBreakpoints(setBreakpoint, clearBreakpoint) 209 210 if err := bps.Set(0x100); err != nil { 211 t.Fatalf("failed to set breakpoint: %v", err) 212 } 213 214 if err := bps.ClearAllByGoRoutineID(1); err != nil { 215 t.Fatalf("failed to clear breakpoint: %v", err) 216 } 217 218 if numCleared != 0 { 219 t.Errorf("wrong number of clear ops: %d", numCleared) 220 } 221 }