server/pet_retirement_combat_test.go (4329B)
1 package main 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "fmt" 7 "testing" 8 "time" 9 ) 10 11 func TestPetRetirementCancelsCombatOnlyAfterCommit(t *testing.T) { 12 for _, reason := range []string{"dismissal", "expiry"} { 13 for _, fault := range []string{"none", "ignored deletion", "cascade failure", "commit failure"} { 14 t.Run(fmt.Sprintf("%s/%s", reason, fault), func(t *testing.T) { 15 world, output := newPetSummonWorld(t) 16 owner, client := world.characters[1], world.clients[1] 17 world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:]) 18 world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond)) 19 pet := world.characters[1000001] 20 updates := make(map[*worldClient][][]byte) 21 world.startAttack(pet, owner, time.Now(), updates) 22 world.startAttack(owner, pet, time.Now(), updates) 23 pet.threat = map[uint32]int64{owner.id: 10} 24 pet.nextAttack, owner.nextAttack = pet.pet.expiresAt.Add(time.Second), pet.pet.expiresAt.Add(2*time.Second) 25 petDeadline, ownerDeadline, health := pet.nextAttack, owner.nextAttack, owner.health 26 query := "CREATE TRIGGER reject_retirement BEFORE DELETE ON characters WHEN OLD.id=1000001 BEGIN SELECT RAISE(IGNORE); END" 27 if fault == "cascade failure" { 28 query = "CREATE TRIGGER reject_retirement BEFORE DELETE ON pets WHEN OLD.npc_id=1000001 BEGIN SELECT RAISE(ABORT,'reject ownership deletion'); END" 29 } 30 if fault == "commit failure" { 31 if _, err := world.db.Exec("CREATE TABLE retirement_commit_failure (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED)"); err != nil { 32 t.Fatal(err) 33 } 34 query = "CREATE TRIGGER reject_retirement AFTER DELETE ON characters WHEN OLD.id=1000001 BEGIN INSERT INTO retirement_commit_failure VALUES (999); END" 35 } 36 if fault != "none" { 37 if _, err := world.db.Exec(query); err != nil { 38 t.Fatal(err) 39 } 40 } 41 retire := func() { 42 if reason == "expiry" { 43 world.advanceNanos(pet.pet.expiresAt) 44 } else if err := world.handlePetCommand(client, petDismissRequest(owner.id)[20:]); err != nil { 45 t.Fatal(err) 46 } 47 } 48 before, err := readTestItemStore(world) 49 if err != nil { 50 t.Fatal(err) 51 } 52 output.packets = nil 53 54 retire() 55 if fault != "none" { 56 after, err := readTestItemStore(world) 57 if err != nil || !bytes.Equal(before, after) || world.characters[pet.id] != pet || !owner.pets[pet.id] || 58 world.fighting[pet.id] != pet || world.fighting[owner.id] != owner || pet.fighting != owner.id || owner.fighting != pet.id || 59 !pet.nextAttack.Equal(petDeadline) || !owner.nextAttack.Equal(ownerDeadline) || len(output.packets) != 0 { 60 t.Fatal("failed retirement changed storage, combat or output", err) 61 } 62 if _, err := world.db.Exec("DROP TRIGGER reject_retirement"); err != nil { 63 t.Fatal(err) 64 } 65 retire() 66 } 67 if world.fighting[pet.id] != nil || world.fighting[owner.id] != nil || pet.fighting != 0 || owner.fighting != 0 || 68 !pet.nextAttack.IsZero() || !owner.nextAttack.IsZero() { 69 t.Fatal("committed retirement retained an encounter or future swing") 70 } 71 world.advanceCombat(ownerDeadline.Add(time.Second)) 72 cold := newServerForTest(t) 73 74 if world.characters[pet.id] != nil || owner.pets[pet.id] || cold.characters[pet.id] != nil || len(cold.characters[1].pets) != 0 || 75 world.fighting[pet.id] != nil || world.fighting[owner.id] != nil || pet.fighting != 0 || owner.fighting != 0 || 76 !pet.nextAttack.IsZero() || !owner.nextAttack.IsZero() || owner.health != health || owner.nano != 25 || world.nextNPC != 1000002 { 77 t.Fatal("retired pet retained combat or changed resources/ownership") 78 } 79 stopped := map[uint32]bool{} 80 for _, packet := range output.packets { 81 opcode := binary.BigEndian.Uint32(packet[16:20]) 82 if opcode == 0x4a41203e { 83 stopped[binary.BigEndian.Uint32(packet[24:28])] = true 84 } 85 if opcode == 0x36510078 && (!stopped[owner.id] || !stopped[pet.id]) { 86 t.Fatal("native destruction preceded encounter cancellation") 87 } 88 if opcode == 0x46002f16 || opcode == 0x5c654b28 { 89 t.Fatal("retired encounter emitted a weapon result") 90 } 91 } 92 if len(stopped) != 2 { 93 t.Fatal("retirement omitted either participant's native stop") 94 } 95 }) 96 } 97 } 98 }