server/zone_checkpoint_test.go (9693B)
1 package main 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 ) 8 9 type zoneCheckpoint time.Time 10 11 func (checkpoint zoneCheckpoint) execute(state *server) error { 12 state.zone.startCheckpoint(state, time.Time(checkpoint)) 13 return nil 14 } 15 16 type zoneCheckpointPosition float32 17 18 func (position zoneCheckpointPosition) execute(state *server) error { 19 state.characters[1].position[0] = float32(position) 20 return nil 21 } 22 23 type zoneCheckpointObservation struct { 24 position, saved float32 25 pending bool 26 nextSave time.Time 27 } 28 29 func (query *zoneCheckpointObservation) execute(state *server) error { 30 query.position = state.characters[1].position[0] 31 query.saved = state.characters[1].savedPosition.Position[0] 32 query.nextSave = state.characters[1].nextPositionSave 33 query.pending = state.zone.checkpoint != nil 34 return nil 35 } 36 37 func TestZoneCheckpointKeepsItsOwnerResponsiveWhileSQLiteWaits(t *testing.T) { 38 fixture := newCheckpointWorld(t) 39 client := &worldClient{id: 1, ready: true, conn: &combatTestConn{}} 40 fixture.clients[1] = client 41 now := time.Now() 42 fixture.characters[1].nextPositionSave = now 43 fixture.characters[1].position[0] = 53 44 zone := newZoneRuntime(fixture) 45 defer func() { 46 if err := zone.stop(); err != nil { 47 t.Error(err) 48 } 49 }() 50 blocker, err := fixture.db.Begin() 51 if err != nil { 52 t.Fatal(err) 53 } 54 defer blocker.Rollback() 55 waits := fixture.db.Stats().WaitCount 56 if err := zone.call(zoneCheckpoint(now)); err != nil { 57 t.Fatal(err) 58 } 59 deadline := time.After(3 * time.Second) 60 poll := time.NewTicker(time.Millisecond) 61 defer poll.Stop() 62 for fixture.db.Stats().WaitCount == waits { 63 select { 64 case <-poll.C: 65 case <-deadline: 66 t.Fatal("checkpoint did not reach SQLite") 67 } 68 } 69 70 responsive := make(chan error, 1) 71 go func() { responsive <- zone.call(zonePacket{client, characterActionPacket(1, [5]uint32{86})[20:]}) }() 72 select { 73 case err := <-responsive: 74 if err != nil { 75 t.Fatal(err) 76 } 77 case <-deadline: 78 t.Fatal("checkpoint blocked commands in its own zone") 79 } 80 if err := zone.call(zoneCheckpointPosition(54)); err != nil { 81 t.Fatal(err) 82 } 83 // Repeated visits cannot create an unbounded backlog behind the pending write. 84 for range 3 { 85 if err := zone.call(zoneCheckpoint(now.Add(time.Second))); err != nil { 86 t.Fatal(err) 87 } 88 } 89 if len(fixture.databaseWriter.requests) != 0 { 90 t.Fatal("zone queued duplicate periodic checkpoints") 91 } 92 if err := blocker.Rollback(); err != nil { 93 t.Fatal(err) 94 } 95 query := &zoneCheckpointObservation{} 96 for { 97 if err := zone.call(query); err != nil { 98 t.Fatal(err) 99 } 100 if !query.pending { 101 break 102 } 103 select { 104 case <-poll.C: 105 case <-deadline: 106 t.Fatal("checkpoint completion did not return to its owner") 107 } 108 } 109 var position float32 110 if err := fixture.db.QueryRow("SELECT position_x FROM characters WHERE id=1").Scan(&position); err != nil { 111 t.Fatal(err) 112 } 113 if position != 53 || query.saved != 53 || query.position != 54 { 114 t.Fatal("checkpoint consumed later live state or lost its completion", position, query) 115 } 116 } 117 118 func TestWorldShutdownDrainsPendingCheckpointThenSavesLatestPosition(t *testing.T) { 119 fixture := newCheckpointWorld(t) 120 now := time.Now() 121 fixture.characters[1].nextPositionSave = now 122 fixture.characters[1].position[0] = 53 123 world := newRoutedTestWorld(t, fixture) 124 finished := make(chan struct{}) 125 var shutdownError error 126 started := false 127 t.Cleanup(func() { 128 if started { 129 <-finished 130 } else if err := world.stop(); err != nil { 131 t.Error(err) 132 } 133 }) 134 source, _ := world.owner(1) 135 blocker, err := fixture.db.Begin() 136 if err != nil { 137 t.Fatal(err) 138 } 139 defer blocker.Rollback() 140 if err := source.call(zoneCheckpoint(now)); err != nil { 141 t.Fatal(err) 142 } 143 if err := source.call(zoneCheckpointPosition(54)); err != nil { 144 t.Fatal(err) 145 } 146 started = true 147 go func() { shutdownError = world.stop(); close(finished) }() 148 select { 149 case <-source.quiesced: 150 case <-time.After(3 * time.Second): 151 t.Fatal("pending checkpoint prevented quiescing") 152 } 153 select { 154 case <-finished: 155 t.Fatal("shutdown returned while SQLite was blocked", shutdownError) 156 default: 157 } 158 if err := blocker.Rollback(); err != nil { 159 t.Fatal(err) 160 } 161 select { 162 case <-finished: 163 if shutdownError != nil { 164 t.Fatal(shutdownError) 165 } 166 case <-time.After(3 * time.Second): 167 t.Fatal("shutdown did not drain its pending checkpoint") 168 } 169 var position float32 170 if err := fixture.db.QueryRow("SELECT position_x FROM characters WHERE id=1").Scan(&position); err != nil || position != 54 { 171 t.Fatal("final checkpoint lost movement after its periodic snapshot", position, err) 172 } 173 } 174 175 func TestWorldShutdownSavesDormantActorsWithoutRewritingInventory(t *testing.T) { 176 fixture := newCheckpointWorld(t) 177 auditCheckpointWrites(t, fixture) 178 now := time.Now() 179 fixture.characters[1].nextPositionSave = now.Add(time.Hour) 180 fixture.characters[1].position[0], fixture.characters[1].health, fixture.characters[1].nano = 53, 17, 11 181 fixture.characters[1].activeNanos = []activeNano{{program: 27169, caster: 1, expiresAt: now.Add(time.Minute)}} 182 fixture.characters[1000000].health = 23 183 world := newRoutedTestWorld(t, fixture) 184 if err := world.stop(); err != nil { 185 t.Fatal(err) 186 } 187 var position float32 188 var health, nano, npcHealth uint32 189 var remaining time.Duration 190 if err := fixture.db.QueryRow(`SELECT position_x,health,nano,(SELECT health FROM characters WHERE id=1000000), 191 (SELECT remaining_ns FROM active_nanos WHERE character_id=1) FROM characters WHERE id=1`).Scan(&position, &health, &nano, &npcHealth, &remaining); err != nil { 192 t.Fatal(err) 193 } 194 if position != 53 || health != 17 || nano != 11 || npcHealth != 23 { 195 t.Fatal("shutdown skipped actors outside their periodic deadline", position, health, nano, npcHealth) 196 } 197 if remaining < 55*time.Second || remaining > time.Minute { 198 t.Fatal("shutdown advanced effect time to force a checkpoint", remaining) 199 } 200 for table, count := range checkpointWrites(t, fixture) { 201 if !strings.HasPrefix(table, "characters/") && count != 0 { 202 t.Fatal("shutdown rewrote significant state already committed by its action", table, count) 203 } 204 } 205 } 206 207 func TestWorldShutdownReportsCheckpointFailureAndStillStopsOtherZones(t *testing.T) { 208 fixture := newCheckpointWorld(t) 209 other := fixture.newCharacter() 210 other.playfield, other.position = 540, [3]float32{40, 14, 50} 211 if err := fixture.saveUser(other); err != nil { 212 t.Fatal(err) 213 } 214 fixture.characters[1].position[0] = 53 215 fixture.characters[1000000].health = 23 216 other.position[0] = 41 217 fixture.characters[1].nextPositionSave, other.nextPositionSave = time.Now().Add(time.Hour), time.Now().Add(time.Hour) 218 if _, err := fixture.db.Exec("CREATE TRIGGER fail_final BEFORE UPDATE ON characters WHEN OLD.id=1000000 BEGIN SELECT RAISE(ABORT,'final checkpoint rejected'); END"); err != nil { 219 t.Fatal(err) 220 } 221 world := newRoutedTestWorld(t, fixture) 222 first, _ := world.owner(1) 223 second, _ := world.owner(other.id) 224 err := world.stop() 225 if err == nil || !strings.Contains(err.Error(), "final checkpoint rejected") { 226 t.Fatal("shutdown hid its checkpoint failure", err) 227 } 228 for _, zone := range []*zoneRuntime{first, second} { 229 select { 230 case <-zone.done: 231 default: 232 t.Fatal("failed checkpoint prevented another zone from stopping") 233 } 234 } 235 var firstPosition, secondPosition float32 236 if err := fixture.db.QueryRow("SELECT (SELECT position_x FROM characters WHERE id=1),(SELECT position_x FROM characters WHERE id=2)").Scan(&firstPosition, &secondPosition); err != nil || firstPosition != 52 || secondPosition != 41 { 237 t.Fatal("shutdown partially published its failed write or skipped the other zone", firstPosition, secondPosition, err) 238 } 239 } 240 241 func TestZoneCheckpointTimerPersistsDueMovement(t *testing.T) { 242 fixture := newCheckpointWorld(t) 243 fixture.characters[1].position[0] = 53 244 fixture.characters[1].nextPositionSave = time.Now() 245 zone := newZoneRuntime(fixture) 246 defer func() { 247 if err := zone.stop(); err != nil { 248 t.Error(err) 249 } 250 }() 251 deadline := time.After(3 * time.Second) 252 poll := time.NewTicker(10 * time.Millisecond) 253 defer poll.Stop() 254 for { 255 var position float32 256 if err := fixture.db.QueryRow("SELECT position_x FROM characters WHERE id=1").Scan(&position); err != nil { 257 t.Fatal(err) 258 } 259 if position == 53 { 260 break 261 } 262 select { 263 case <-poll.C: 264 case <-deadline: 265 t.Fatal("zone loop did not checkpoint due movement") 266 } 267 } 268 } 269 270 func TestZoneCheckpointFailureRetainsItsBasisAndRetries(t *testing.T) { 271 fixture := newCheckpointWorld(t) 272 now := time.Now() 273 fixture.characters[1].position[0], fixture.characters[1].nextPositionSave = 53, now 274 if _, err := fixture.db.Exec("CREATE TRIGGER fail_periodic BEFORE UPDATE ON characters WHEN OLD.id=1 BEGIN SELECT RAISE(ABORT,'periodic checkpoint rejected'); END"); err != nil { 275 t.Fatal(err) 276 } 277 zone := newZoneRuntime(fixture) 278 defer func() { 279 if err := zone.stop(); err != nil { 280 t.Error(err) 281 } 282 }() 283 deadline := time.After(3 * time.Second) 284 poll := time.NewTicker(time.Millisecond) 285 defer poll.Stop() 286 query := &zoneCheckpointObservation{} 287 for attempt := range 2 { 288 if err := zone.call(zoneCheckpoint(now)); err != nil { 289 t.Fatal(err) 290 } 291 for { 292 if err := zone.call(query); err != nil { 293 t.Fatal(err) 294 } 295 if !query.pending { 296 break 297 } 298 select { 299 case <-poll.C: 300 case <-deadline: 301 t.Fatal("failed checkpoint did not return to its owner") 302 } 303 } 304 if attempt == 0 { 305 if query.saved != 52 || query.position != 53 { 306 t.Fatal("failed checkpoint published a saved basis", query) 307 } 308 if _, err := fixture.db.Exec("DROP TRIGGER fail_periodic"); err != nil { 309 t.Fatal(err) 310 } 311 } else if query.saved != 53 || query.position != 53 { 312 t.Fatal("checkpoint did not retry after the failure", query) 313 } 314 } 315 }