aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/corpse_expiry_test.go (9143B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"slices"
      7 	"sync"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func newExpiringCorpseWorld(t *testing.T) (*server, *combatTestConn, time.Time) {
     13 	t.Helper()
     14 	world, conn := newRespawnWorld(t)
     15 	if err := world.saveUser(world.characters[1]); err != nil {
     16 		t.Fatal(err)
     17 	}
     18 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
     19 	killed := world.characters[1].nextAttack
     20 	world.advanceCombat(killed)
     21 	world.characters[1000000].corpse.items = []ownedItem{{ID: 17, Type: 51005, Template: 42640, Quality: 1, Count: 1}}
     22 	if err := world.flushWorld(); err != nil {
     23 		t.Fatal(err)
     24 	}
     25 	return world, conn, killed
     26 }
     27 
     28 func TestCorpseExpiryClosesLootAndRemovesOnlyTheDeadLifeForBothObservers(t *testing.T) {
     29 	world, owner, killed := newExpiringCorpseWorld(t)
     30 	observer := world.newCharacter()
     31 	other := &combatTestConn{}
     32 	world.clients[observer.id] = &worldClient{id: observer.id, conn: other, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     33 	world.handleLootUse(world.clients[1], lootUseRequest(1))
     34 	if world.characters[1000000].corpse.expiresAt != killed.Add(180*time.Second) {
     35 		t.Fatal("corpse lifetime did not come from the profile and killing blow")
     36 	}
     37 	world.advanceNPCRespawns(killed.Add(100 * time.Second))
     38 	owner.packets, other.packets = nil, nil
     39 
     40 	world.advanceCorpseExpiry(killed.Add(180*time.Second - time.Nanosecond))
     41 	if world.characters[1000000] == nil || len(owner.packets) != 0 {
     42 		t.Fatal("corpse disappeared before its deadline")
     43 	}
     44 	world.advanceCorpseExpiry(killed.Add(180 * time.Second))
     45 	world.advanceCorpseExpiry(killed.Add(time.Hour))
     46 
     47 	if world.characters[1000000] != nil || world.clients[1].lootCorpse != 0 ||
     48 		world.clients[1].npcs[1000000] || world.clients[2].npcs[1000000] || len(world.corpseExpiry) != 0 {
     49 		t.Fatal("expired corpse retained its actor, loot window, interest or expiry entry")
     50 	}
     51 	if len(world.characters) != 3 || world.characters[1000001].health != 20 ||
     52 		len(world.mobileNPCs[950]) != 1 || world.mobileNPCs[950][0].id != 1000001 {
     53 		t.Fatal("corpse removal changed the living replacement")
     54 	}
     55 	if len(owner.packets) != 3 || len(other.packets) != 2 ||
     56 		binary.BigEndian.Uint32(owner.packets[0][16:20]) != 0x2049527c ||
     57 		binary.BigEndian.Uint32(owner.packets[0][33:37]) != 102 {
     58 		t.Fatal("expiry did not close the owner's native container before deletion")
     59 	}
     60 	for index, identity := range []uint32{50000, 51050} {
     61 		packet := owner.packets[index+1]
     62 		if binary.BigEndian.Uint32(packet[16:20]) != 0x36510078 || binary.BigEndian.Uint32(packet[20:24]) != identity ||
     63 			binary.BigEndian.Uint32(packet[24:28]) != 1000000 || !bytes.Equal(packet[16:], other.packets[index][16:]) {
     64 			t.Fatalf("observers did not delete the same hidden actor and corpse: %x", packet)
     65 		}
     66 	}
     67 }
     68 
     69 func TestCorpseExpiryRejectsUseAndPickupEvenBeforeTheCleanupTick(t *testing.T) {
     70 	world, conn, killed := newExpiringCorpseWorld(t)
     71 	world.handleLootUse(world.clients[1], lootUseRequest(1))
     72 	world.characters[1000000].corpse.expiresAt = time.Now().Add(-time.Second)
     73 	conn.packets = nil
     74 
     75 	world.handleLootUse(world.clients[1], lootUseRequest(2))
     76 	world.handleInventory(world.clients[1], lootTakeRequest())
     77 	if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 2 ||
     78 		len(world.characters[1].inventory.Items) != 0 || len(world.characters[1000000].corpse.items) != 1 {
     79 		t.Fatal("an expired corpse accepted a request between scheduler visits")
     80 	}
     81 	world.advanceCorpseExpiry(killed.Add(180 * time.Second))
     82 	world.handleLootUse(world.clients[1], lootUseRequest(3))
     83 	world.handleInventory(world.clients[1], lootTakeRequest())
     84 	world.advanceNPCs(killed.Add(181 * time.Second))
     85 	restarted := newTestInventoryServer(t)
     86 
     87 	if world.characters[1000000] != nil || world.clients[1].lootCorpse != 0 || world.clients[1].npcs[1000000] ||
     88 		len(world.characters[1].inventory.Items) != 0 || len(restarted.characters[1].inventory.Items) != 0 {
     89 		t.Fatal("stale use, pickup, reentry or restart recovered an expired item")
     90 	}
     91 }
     92 
     93 func TestCorpseExpiryPreservesAlreadyTakenInventoryAfterRestart(t *testing.T) {
     94 	world, _, killed := newExpiringCorpseWorld(t)
     95 	world.handleLootUse(world.clients[1], lootUseRequest(1))
     96 	if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil {
     97 		t.Fatal(err)
     98 	}
     99 
    100 	world.advanceCorpseExpiry(killed.Add(180 * time.Second))
    101 	world.handleInventory(world.clients[1], lootTakeRequest())
    102 	restarted := newTestInventoryServer(t)
    103 
    104 	expected := []ownedItem{{ID: 17, Type: 51005, Template: 42640, Quality: 1, Slot: 64, Count: 1}}
    105 	if world.characters[1000000] != nil || !slices.Equal(world.characters[1].inventory.Items, expected) ||
    106 		!slices.Equal(restarted.characters[1].inventory.Items, expected) {
    107 		t.Fatal("expiry lost or duplicated the item that had already changed owners")
    108 	}
    109 }
    110 
    111 func TestCorpseExpiryBeforeRespawnWithoutObserversKeepsOneRoamingReplacement(t *testing.T) {
    112 	world, conn := newRespawnWorld(t)
    113 	profile := *world.characters[1000000].combatProfile
    114 	profile.CorpseSeconds = 1
    115 	world.characters[1000000].combatProfile = &profile
    116 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
    117 	killed := world.characters[1].nextAttack
    118 	world.advanceCombat(killed)
    119 	client := world.clients[1]
    120 	delete(world.clients, 1)
    121 	conn.packets = nil
    122 
    123 	world.advanceCorpseExpiry(killed.Add(time.Second))
    124 	if world.characters[1000000] != nil || len(world.mobileNPCs[950]) != 0 || len(world.npcRespawns) != 1 {
    125 		t.Fatal("early corpse expiry retained the dead actor or discarded its pending respawn")
    126 	}
    127 	world.advanceNPCRespawns(killed.Add(100 * time.Second))
    128 	if len(conn.packets) != 0 {
    129 		t.Fatal("an unavailable observer received expiry or respawn packets")
    130 	}
    131 	world.clients[1] = client
    132 	client.npcs = make(map[uint32]bool)
    133 	world.advanceNPCs(killed.Add(101 * time.Second))
    134 
    135 	if len(world.characters) != 2 || world.characters[1000001].health != 20 || len(world.npcRespawns) != 0 ||
    136 		len(world.mobileNPCs[950]) != 1 || world.mobileNPCs[950][0].id != 1000001 ||
    137 		len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x271b3a6b ||
    138 		binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x1d3c0f1c {
    139 		t.Fatal("respawn or reentry lost the roaming replacement or restored its expired corpse")
    140 	}
    141 }
    142 
    143 func TestCorpseExpiryConcurrentPickupHasAtMostOneDurableOwner(t *testing.T) {
    144 	world, _, killed := newExpiringCorpseWorld(t)
    145 	world.handleLootUse(world.clients[1], lootUseRequest(1))
    146 	var workers sync.WaitGroup
    147 	errors := make(chan error, 8)
    148 
    149 	client := world.clients[1]
    150 	zone, stop := startTestZone(t, world)
    151 
    152 	workers.Go(func() {
    153 		if err := zone.call(zoneTestCommand(func(state *server) error {
    154 			state.advanceCorpseExpiry(killed.Add(180 * time.Second))
    155 			return nil
    156 		})); err != nil {
    157 			t.Error(err)
    158 		}
    159 	})
    160 	for range 8 {
    161 		workers.Go(func() { errors <- zone.call(zonePacket{client, lootTakeRequest()}) })
    162 	}
    163 	workers.Wait()
    164 	if err := stop(); err != nil {
    165 		t.Fatal(err)
    166 	}
    167 	close(errors)
    168 	for err := range errors {
    169 		if err != nil {
    170 			t.Fatal(err)
    171 		}
    172 	}
    173 	restarted := newTestInventoryServer(t)
    174 
    175 	items := world.characters[1].inventory.Items
    176 	if world.characters[1000000] != nil || len(items) > 1 || !slices.Equal(items, restarted.characters[1].inventory.Items) ||
    177 		(len(items) == 1 && items[0] != (ownedItem{ID: 17, Type: 51005, Template: 42640, Quality: 1, Slot: 64, Count: 1})) {
    178 		t.Fatal("a pickup racing expiry duplicated or lost durable ownership")
    179 	}
    180 }
    181 
    182 func TestCorpseWithoutExpiryProfileKeepsItsExistingPolicy(t *testing.T) {
    183 	world, _ := newLootWorld(t)
    184 
    185 	world.advanceCorpseExpiry(time.Now().Add(time.Hour))
    186 
    187 	if world.characters[1000000] == nil || !world.characters[1000000].corpse.expiresAt.IsZero() {
    188 		t.Fatal("an unprofiled corpse received an invented lifetime")
    189 	}
    190 }
    191 
    192 func TestCorpseExpiryRetiresKnownObjectsWhileTheObserverIsLoading(t *testing.T) {
    193 	world, conn, killed := newExpiringCorpseWorld(t)
    194 	world.clients[1].ready = false
    195 	world.clients[1].loading = true
    196 	conn.packets = nil
    197 
    198 	world.advanceCorpseExpiry(killed.Add(180 * time.Second))
    199 	world.clients[1].ready = true
    200 	world.clients[1].loading = false
    201 	world.advanceNPCs(killed.Add(181 * time.Second))
    202 
    203 	if len(conn.packets) != 2 || world.clients[1].npcs[1000000] ||
    204 		binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x36510078 || binary.BigEndian.Uint32(conn.packets[0][20:24]) != 50000 ||
    205 		binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x36510078 || binary.BigEndian.Uint32(conn.packets[1][20:24]) != 51050 {
    206 		t.Fatal("an object sent during initialization survived expiry as a client-only corpse")
    207 	}
    208 }
    209 
    210 func TestCorpseExpiryFixedCreatureDoesNotRequireARoamingRoster(t *testing.T) {
    211 	world, _, killed := newExpiringCorpseWorld(t)
    212 	world.characters[1000000].motion = nil
    213 	world.mobileNPCs = nil
    214 
    215 	world.advanceCorpseExpiry(killed.Add(180 * time.Second))
    216 	world.advanceNPCRespawns(killed.Add(181 * time.Second))
    217 
    218 	if world.characters[1000000] != nil || world.characters[1000001].health != 20 ||
    219 		world.characters[1000001].motion != nil || len(world.corpseExpiry) != 0 || len(world.npcRespawns) != 0 {
    220 		t.Fatal("fixed corpse cleanup or its replacement required an unrelated roaming roster")
    221 	}
    222 }