server/resurrection_reconnect_test.go (10125B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "net" 8 "reflect" 9 "testing" 10 ) 11 12 func pendingResurrectionWorld(t *testing.T, saved bool) *server { 13 t.Helper() 14 world, _ := newBuffedDeathWorld(t) 15 player := world.characters[1] 16 player.level, player.experience, player.insuredExperience = 20, 1500, 140850 17 player.playfield, player.position = 950, [3]float32{178, 15, 57} 18 if saved { 19 player.resurrectionTerminal = 0xc00202c1 20 } 21 if err := world.saveUser(player); err != nil { 22 t.Fatal(err) 23 } 24 if err := world.savePosition(player); err != nil { 25 t.Fatal(err) 26 } 27 defeatReclaimPlayer(t, world) 28 delete(world.clients, 1) 29 return world 30 } 31 32 func reconnectResurrectionPlayer(t *testing.T, world *server, messages ...[]byte) [][]byte { 33 t.Helper() 34 loadResurrectionLocations(t, world) 35 host, peer := net.Pipe() 36 defer host.Close() 37 defer peer.Close() 38 cookie := systemPacket(0x1b, []byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}) 39 binary.BigEndian.PutUint16(cookie, 0xdfdf) 40 for _, message := range messages { 41 cookie = append(cookie, targetPacket(append([]byte(nil), message...))...) 42 } 43 conn := &combatInterestConn{combatTestConn: combatTestConn{Conn: host}, input: bytes.NewReader(cookie)} 44 if err := serveWorldClient(world, &worldClient{conn: conn, loading: true}); err != nil { 45 t.Fatal(err) 46 } 47 return conn.packets 48 } 49 50 func TestResurrectionReconnectResumesPendingDeath(t *testing.T) { 51 for _, restart := range []bool{false, true} { 52 for _, saved := range []bool{false, true} { 53 t.Run(fmt.Sprintf("restart=%t/saved=%t", restart, saved), func(t *testing.T) { 54 world := pendingResurrectionWorld(t, saved) 55 if restart { 56 if err := world.closeDatabase(); err != nil { 57 t.Fatal(err) 58 } 59 world = newTestInventoryServer(t) 60 } 61 player := world.characters[1] 62 belongings := player.inventory 63 durableBelongings := newTestInventoryServer(t).characters[1].inventory 64 observer := &combatTestConn{} 65 world.characters[2] = &character{id: 2, playfield: 950, position: player.position, health: 100} 66 world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true} 67 if _, err := world.db.Exec("CREATE TABLE resurrection_writes (health INTEGER); CREATE TRIGGER record_resurrection AFTER UPDATE OF health ON characters WHEN NEW.id=1 AND OLD.health=0 AND NEW.health>0 BEGIN INSERT INTO resurrection_writes VALUES (NEW.health); END"); err != nil { 68 t.Fatal(err) 69 } 70 ready := n3Packet(0x570c2039, 50000, 1, nil) 71 movement := *player 72 movement.position[0] = 999 73 74 packets := reconnectResurrectionPlayer(t, world, resurrectionRequest(1), ready, ready, npcMovementPacket(&movement, 2)) 75 restored := newTestInventoryServer(t).characters[1] 76 77 if player.health != 0 || restored.health != 0 || player.playfield != 950 || player.position != [3]float32{178, 15, 57} || 78 player.experience != 1000 || restored.experience != 1000 || player.insuredExperience != 140850 || 79 len(player.activeNanos) != 0 || !reflect.DeepEqual(player.inventory, belongings) { 80 t.Fatal("reconnect bypassed death or changed its committed outcome") 81 } 82 death, seenReady, seenFull := 0, false, false 83 var observerStats, ownStats [][2]uint32 84 for _, packet := range packets { 85 switch binary.BigEndian.Uint32(packet[16:20]) { 86 case 0x5f52412e: 87 seenFull = true 88 case 0x570c2039: 89 seenReady = true 90 case 0x5e477770: 91 if binary.BigEndian.Uint32(packet[29:33]) == 99 { 92 if !seenFull || !seenReady { 93 t.Fatal("death replay preceded the loaded local actor") 94 } 95 death++ 96 } 97 case 0x2b333d6e: 98 if binary.BigEndian.Uint32(packet[24:28]) == 1 { 99 ownStats = append(ownStats, [2]uint32{binary.BigEndian.Uint32(packet[29:33]), binary.BigEndian.Uint32(packet[33:37])}) 100 continue 101 } 102 if binary.BigEndian.Uint32(packet[24:28]) != 2 { 103 t.Fatalf("pending reconnect changed the dead player's stats: %x", packet) 104 } 105 observerStats = append(observerStats, [2]uint32{binary.BigEndian.Uint32(packet[29:33]), binary.BigEndian.Uint32(packet[33:37])}) 106 case 0x445f2a0b, 0x43197d22: 107 t.Fatalf("pending reconnect replayed resurrection or a stat outcome: %x", packet) 108 } 109 } 110 expectedStats := [][2]uint32{{16, 6}, {17, 6}, {18, 6}, {19, 6}, {20, 6}, {21, 6}, 111 {138, 5}, {118, 5}, {119, 5}, {120, 5}, {149, 5}, {51, 0}, {425, 100}, {430, 0}, {78, 0}, {156, 5}} 112 if !reflect.DeepEqual(observerStats, expectedStats) { 113 t.Fatal("pending reconnect omitted or changed the other player's combat foundations", observerStats) 114 } 115 if !reflect.DeepEqual(ownStats, [][2]uint32{{1, player.maximumHealth()}, {221, player.maximumNano()}, {22, 7}}) { 116 t.Fatal("pending reconnect changed its resource or attack foundations", ownStats) 117 } 118 if death != 1 { 119 t.Fatalf("repeated ready produced %d death notifications", death) 120 } 121 observerDeaths := 0 122 for _, packet := range observer.packets { 123 if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 { 124 observerDeaths++ 125 } 126 } 127 if observerDeaths != 1 { 128 t.Fatalf("observer saw %d pending-death replays", observerDeaths) 129 } 130 131 packets = reconnectResurrectionPlayer(t, world, ready, resurrectionRequest(1), resurrectionRequest(1)) 132 restored = newTestInventoryServer(t).characters[1] 133 var writes int 134 if err := world.db.QueryRow("SELECT COUNT(*) FROM resurrection_writes").Scan(&writes); err != nil { 135 t.Fatal(err) 136 } 137 138 position := [3]float32{52.597332, 14.75, 57.636177} 139 playfield := uint32(950) 140 if saved { 141 position = [3]float32{918, 20.7, 578} 142 playfield = 705 143 } 144 if writes != 1 || player.health != 1 || restored.health != 1 || restored.nano != 1 || restored.playfield != playfield || restored.position != position || 145 restored.experience != 1000 || restored.insuredExperience != 140850 || restored.stats.ImprovementPoints != 1500 || 146 len(restored.activeNanos) != 0 || !reflect.DeepEqual(restored.inventory, durableBelongings) { 147 t.Fatal("resurrection lost durable state or restored resources more than once") 148 } 149 var transition []uint32 150 for _, packet := range packets { 151 key := binary.BigEndian.Uint32(packet[16:20]) 152 if key == 0x445f2a0b || key == 0x43197d22 || key == 0x3c { 153 transition = append(transition, key) 154 } 155 } 156 if !reflect.DeepEqual(transition, []uint32{0x445f2a0b, 0x43197d22, 0x3c}) { 157 t.Fatalf("resurrection transition missing or repeated: %x", transition) 158 } 159 160 packets = reconnectResurrectionPlayer(t, world, ready, resurrectionRequest(1)) 161 162 if player.health != 1 || player.position != position { 163 t.Fatal("arrival reconnect restored full health or changed the return point") 164 } 165 for _, packet := range packets { 166 key := binary.BigEndian.Uint32(packet[16:20]) 167 if key == 0x445f2a0b || key == 0x43197d22 || key == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 { 168 t.Fatal("living reconnect replayed death or resurrection") 169 } 170 } 171 }) 172 } 173 } 174 } 175 176 func TestResurrectionReconnectBeforeReadyKeepsPendingDeath(t *testing.T) { 177 world := pendingResurrectionWorld(t, true) 178 179 packets := reconnectResurrectionPlayer(t, world, resurrectionRequest(1)) 180 restored := newTestInventoryServer(t).characters[1] 181 182 if world.characters[1].health != 0 || restored.health != 0 || restored.playfield != 950 || restored.experience != 1000 { 183 t.Fatal("unfinished login bypassed death") 184 } 185 for _, packet := range packets { 186 key := binary.BigEndian.Uint32(packet[16:20]) 187 if key == 0x445f2a0b || key == 0x43197d22 || key == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 99 { 188 t.Fatal("unfinished login resumed presentation before ready") 189 } 190 } 191 } 192 193 func TestResurrectionPendingDeathPersistenceFailureCanRetry(t *testing.T) { 194 for _, failure := range []struct{ name, statement string }{ 195 {"update", "CREATE TRIGGER reject_return BEFORE UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN SELECT RAISE(ABORT, 'return'); END"}, 196 {"ignored update", "CREATE TRIGGER reject_return BEFORE UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN SELECT RAISE(IGNORE); END"}, 197 {"commit", "CREATE TABLE pending_return (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER reject_return AFTER UPDATE OF health ON characters WHEN NEW.id=1 AND NEW.health>0 BEGIN INSERT INTO pending_return VALUES (999999); END"}, 198 } { 199 t.Run(failure.name, func(t *testing.T) { 200 pendingResurrectionWorld(t, true) 201 world := newTestInventoryServer(t) 202 player := world.characters[1] 203 belongings := player.inventory 204 conn := &combatTestConn{} 205 client := &worldClient{id: 1, ready: true, target: true, conn: conn} 206 world.clients[1] = client 207 if _, err := world.db.Exec(failure.statement); err != nil { 208 t.Fatal(err) 209 } 210 211 err := world.handleResurrection(client, resurrectionRequest(1)[20:]) 212 restored := newTestInventoryServer(t).characters[1] 213 214 if err == nil || player.health != 0 || restored.health != 0 || restored.playfield != 950 || restored.position != [3]float32{178, 15, 57} || 215 restored.experience != 1000 || restored.insuredExperience != 140850 || len(restored.activeNanos) != 0 || 216 !reflect.DeepEqual(restored.inventory, belongings) || world.clients[1] != client || !client.ready || len(conn.packets) != 0 { 217 t.Fatal("failed return changed the pending death or published success", err) 218 } 219 if _, err := world.db.Exec("DROP TRIGGER reject_return"); err != nil { 220 t.Fatal(err) 221 } 222 223 err = world.handleResurrection(client, resurrectionRequest(1)[20:]) 224 restored = newTestInventoryServer(t).characters[1] 225 226 if err != nil || restored.health != 1 || restored.nano != 1 || restored.playfield != 705 || restored.position != [3]float32{918, 20.7, 578} || 227 restored.experience != 1000 || !reflect.DeepEqual(restored.inventory, belongings) || len(conn.packets) != 3 { 228 t.Fatal("retry did not complete exactly one durable scanner return", err) 229 } 230 }) 231 } 232 }