server/fixture_database_test.go (5970B)
1 package main 2 3 import ( 4 "maps" 5 "net" 6 "testing" 7 "time" 8 ) 9 10 func newServerForTest(t *testing.T) *server { 11 t.Helper() 12 world := newServerBase() 13 // A persisted serial fixture may intentionally contain only a few hand-built 14 // actors with IDs inside the real catalog's range. Restore that exact fixture. 15 var initialized bool 16 if err := world.db.QueryRow("SELECT npcs_initialized FROM world WHERE id=1").Scan(&initialized); err != nil { 17 t.Fatal(err) 18 } 19 if initialized { 20 world.populations = make(map[uint32]npcPopulation) 21 } 22 if err := world.loadInitialState(); err != nil { 23 t.Fatal(err) 24 } 25 registerServerFixture(t, world) 26 loadTestPopulations(t, world) 27 return world 28 } 29 30 func newDormantServerForTest(t *testing.T) *server { 31 t.Helper() 32 world := newServer() 33 registerServerFixture(t, world) 34 return world 35 } 36 37 func registerServerFixture(t *testing.T, world *server) { 38 t.Helper() 39 geometry := world.geometry 40 t.Cleanup(func() { 41 world.stopNavigation() 42 if err := geometry.close(); err != nil { 43 t.Error(err) 44 } 45 }) 46 if _, synthetic := resources.(npcFixtureResources); synthetic { 47 // Artificial terrain grids do not describe the original outdoor scenery. 48 world.geometry.playfields = nil 49 } 50 registerTestDatabaseCleanup(t, world) 51 } 52 53 func openTestDatabase(t *testing.T, world *server) { 54 t.Helper() 55 if _, err := world.openDatabase(); err != nil { 56 t.Fatal(err) 57 } 58 registerTestDatabaseCleanup(t, world) 59 } 60 61 func registerTestDatabaseCleanup(t *testing.T, world *server) { 62 t.Helper() 63 t.Cleanup(func() { 64 world.stopNavigation() 65 if world.browserStreamService != nil { 66 world.browserStreamService.stop() 67 } 68 if world.db == nil { 69 return 70 } 71 if err := world.closeDatabase(); err != nil { 72 t.Error(err) 73 } 74 if connections := world.db.Stats().OpenConnections; connections != 0 { 75 t.Errorf("fixture retained %d database connections after Close", connections) 76 } 77 }) 78 } 79 80 func advanceNavigationForTest(t *testing.T, world *server, now time.Time, advance func(time.Time)) { 81 t.Helper() 82 // Controlled clocks wait for search completion instead of racing simulated 83 // seconds against real worker time. Apply results through the normal tick. 84 for { 85 advance(now) 86 var pending []*navigationJob 87 for _, actor := range world.characters { 88 if actor.motion != nil && actor.motion.navigation != nil { 89 pending = append(pending, actor.motion.navigation) 90 } 91 } 92 if len(pending) == 0 { 93 return 94 } 95 for _, job := range pending { 96 select { 97 case <-job.done: 98 case <-time.After(10 * time.Second): 99 t.Fatal("navigation worker did not finish") 100 } 101 } 102 } 103 } 104 105 func TestReloadFixtureClosesEveryDatabaseAtScopeExit(t *testing.T) { 106 t.Chdir(t.TempDir()) 107 var first, second *server 108 109 if !t.Run("reload", func(t *testing.T) { 110 first = newServerForTest(t) 111 second = newServerForTest(t) 112 }) { 113 return 114 } 115 116 if first.db.Stats().OpenConnections != 0 || second.db.Stats().OpenConnections != 0 { 117 t.Fatalf("reload fixture retained database connections: first=%d second=%d", 118 first.db.Stats().OpenConnections, second.db.Stats().OpenConnections) 119 } 120 } 121 122 func TestFixtureDisconnectsWorldBeforeClosingDatabase(t *testing.T) { 123 seed, _ := newNanoBuffWorld(t) 124 castTestBuff(t, seed, 1, 1, 27169) 125 var world *server 126 var player *character 127 stopped := make(chan struct{}) 128 peer, connection := net.Pipe() 129 defer peer.Close() 130 defer connection.Close() 131 132 if !t.Run("connected", func(t *testing.T) { 133 world = newTestInventoryServer(t) 134 player = world.characters[1] 135 startTestService(t, world) 136 peer.SetDeadline(time.Now().Add(5 * time.Second)) 137 go func() { 138 defer close(stopped) 139 defer connection.Close() 140 world.serveWorldConnection(connection) 141 }() 142 if _, err := peer.Write(systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0})); err != nil { 143 t.Fatal(err) 144 } 145 readWorldUpdates(t, peer) 146 var running bool 147 callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { 148 running = !state.characters[1].activeNanos[0].expiresAt.IsZero() 149 return nil 150 })) 151 if !running { 152 t.Fatal("login did not resume the saved nano") 153 } 154 }) { 155 return 156 } 157 158 select { 159 case <-stopped: 160 case <-time.After(time.Second): 161 t.Fatal("fixture retained its world session") 162 } 163 if len(world.clients) != 0 || world.db.Stats().OpenConnections != 0 || !player.activeNanos[0].expiresAt.IsZero() { 164 t.Fatal("fixture closed SQLite before retiring the session and pausing its nano") 165 } 166 paused := newTestInventoryServer(t).characters[1].activeNanos[0] 167 if paused.remaining != player.activeNanos[0].remaining || !paused.expiresAt.IsZero() { 168 t.Fatal("fixture logout did not persist the paused nano duration") 169 } 170 } 171 172 // Serial model fixtures materialize the same populations that live zones load 173 // on demand. They keep direct entity access without a second runtime path. 174 func loadTestPopulations(t *testing.T, world *server) { 175 t.Helper() 176 zones := make(map[uint32]bool) 177 for zone := range world.populations { 178 zones[zone] = true 179 } 180 for _, zone := range world.npcOwners { 181 zones[zone] = true 182 } 183 if world.savedNPCs == nil { 184 world.savedNPCs = make(map[uint32]savedNPC) 185 } 186 for zone := range zones { 187 draft := world.emptyZone() 188 draft.playfield = zone 189 draft.items = world.items 190 for id, placement := range world.itemPlacements { 191 if placement.Corpse != 0 && world.npcOwners[placement.Corpse] == zone { 192 draft.itemPlacements[id] = placement 193 } 194 } 195 if err := draft.activatePopulation(); err != nil { 196 t.Fatal(err) 197 } 198 for _, npc := range draft.characters { 199 world.addNPC(npc) 200 } 201 maps.Copy(world.savedNPCs, draft.savedNPCs) 202 maps.Copy(world.mobileNPCs, draft.mobileNPCs) 203 world.npcRespawns = append(world.npcRespawns, draft.npcRespawns...) 204 world.corpseExpiry = append(world.corpseExpiry, draft.corpseExpiry...) 205 world.petExpiry = append(world.petExpiry, draft.petExpiry...) 206 } 207 world.populationLoaded = true 208 world.expirePets(time.Now(), nil) 209 if err := world.expireMissions(time.Now(), nil); err != nil { 210 t.Fatal(err) 211 } 212 }