aorecomp

Anarchy Online classic in the browser

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


server/pet_dismiss_test.go (10213B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"sync"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func petDismissRequest(owner uint32) []byte {
     11 	packet := petFollowRequest(owner)
     12 	binary.BigEndian.PutUint32(packet[36:], 10)
     13 	return packet
     14 }
     15 
     16 func TestPetDismissCommitsOneRemovalAndStopsOldMotion(t *testing.T) {
     17 	world, output := newPetSummonWorld(t)
     18 	player, client := world.characters[1], world.clients[1]
     19 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
     20 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     21 	pet := world.characters[1000001]
     22 	world.handlePetCommand(client, petFollowRequest(1)[20:])
     23 	player.position = [3]float32{10, 0, 0}
     24 	output.packets = nil
     25 	auditCheckpointWrites(t, world)
     26 
     27 	err := world.handlePetCommand(client, petDismissRequest(1)[20:])
     28 	world.handlePetCommand(client, petDismissRequest(1)[20:])
     29 	now := time.Now()
     30 	for tick := 1; tick <= 60; tick++ {
     31 		advanceNavigationForTest(t, world, now.Add(time.Duration(tick)*time.Second/60), world.advancePlayerMovement)
     32 	}
     33 	cold := newServerForTest(t)
     34 
     35 	if err != nil || len(player.pets) != 0 || world.characters[pet.id] != nil || len(world.petExpiry) != 0 ||
     36 		client.npcs[pet.id] || cold.characters[pet.id] != nil || len(cold.characters[1].pets) != 0 ||
     37 		player.nano != 25 || world.nextNPC != 1000002 || pet.position != [3]float32{2, 0, 0} {
     38 		t.Fatal("dismissal did not retire only the owned pet durably", err, len(player.pets), len(world.petExpiry), pet.position)
     39 	}
     40 	if writes := checkpointWrites(t, world); writes["characters/DELETE"] != 1 || writes["npcs/DELETE"] != 1 {
     41 		t.Fatal("dismissal did not delete its entity exactly once", writes)
     42 	}
     43 	if len(output.packets) != 3 || binary.BigEndian.Uint32(output.packets[0][16:20]) != 0x58742a0f ||
     44 		binary.BigEndian.Uint32(output.packets[0][24:28]) != 1 || binary.BigEndian.Uint32(output.packets[0][33:37]) != pet.id ||
     45 		binary.BigEndian.Uint32(output.packets[1][29:33]) != 251 || binary.BigEndian.Uint32(output.packets[1][33:37]) != 0 ||
     46 		binary.BigEndian.Uint32(output.packets[2][16:20]) != 0x36510078 || binary.BigEndian.Uint32(output.packets[2][24:28]) != pet.id {
     47 		t.Fatalf("dismissal did not remove native ownership and the actor exactly once: %x", output.packets)
     48 	}
     49 }
     50 
     51 func TestPetDismissFailurePreservesOwnershipAndRetries(t *testing.T) {
     52 	for _, scenario := range []string{"ignored deletion", "cascade failure", "commit failure"} {
     53 		t.Run(scenario, func(t *testing.T) {
     54 			world, output := newPetSummonWorld(t)
     55 			player, client := world.characters[1], world.clients[1]
     56 			world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
     57 			world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     58 			pet := world.characters[1000001]
     59 			query := "CREATE TRIGGER reject_dismiss BEFORE DELETE ON characters WHEN OLD.id=1000001 BEGIN SELECT RAISE(IGNORE); END"
     60 			if scenario == "cascade failure" {
     61 				query = "CREATE TRIGGER reject_dismiss BEFORE DELETE ON pets WHEN OLD.npc_id=1000001 BEGIN SELECT RAISE(ABORT,'reject ownership deletion'); END"
     62 			}
     63 			if scenario == "commit failure" {
     64 				if _, err := world.db.Exec("CREATE TABLE dismiss_commit_failure (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED)"); err != nil {
     65 					t.Fatal(err)
     66 				}
     67 				query = "CREATE TRIGGER reject_dismiss AFTER DELETE ON characters WHEN OLD.id=1000001 BEGIN INSERT INTO dismiss_commit_failure VALUES (999); END"
     68 			}
     69 			if _, err := world.db.Exec(query); err != nil {
     70 				t.Fatal(err)
     71 			}
     72 			output.packets = nil
     73 
     74 			err := world.handlePetCommand(client, petDismissRequest(1)[20:])
     75 			cold := newServerForTest(t)
     76 
     77 			if err != nil || world.characters[pet.id] != pet || !player.pets[pet.id] || len(world.petExpiry) != 1 ||
     78 				!client.npcs[pet.id] || len(output.packets) != 0 || player.nano != 25 || len(cold.characters[1].pets) != 1 {
     79 				t.Fatal("failed retirement changed ownership or reported success", err, len(output.packets))
     80 			}
     81 			if _, err := world.db.Exec("DROP TRIGGER reject_dismiss"); err != nil {
     82 				t.Fatal(err)
     83 			}
     84 			if err := world.handlePetCommand(client, petDismissRequest(1)[20:]); err != nil {
     85 				t.Fatal(err)
     86 			}
     87 			if len(player.pets) != 0 || world.characters[pet.id] != nil || len(output.packets) != 3 || len(world.petExpiry) != 0 {
     88 				t.Fatal("dismissal could not retry after persistence recovered")
     89 			}
     90 		})
     91 	}
     92 }
     93 
     94 func TestPetDismissRejectsForeignOwnerAndSelectors(t *testing.T) {
     95 	for _, scenario := range []string{"foreign actor", "other owner", "stale client", "loading", "not ready", "selector"} {
     96 		t.Run(scenario, func(t *testing.T) {
     97 			world, output := newPetSummonWorld(t)
     98 			client := world.clients[1]
     99 			world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    100 			world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    101 			request := petDismissRequest(1)[20:]
    102 			switch scenario {
    103 			case "foreign actor", "other owner":
    104 				world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    105 				client = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, conn: &combatTestConn{}}
    106 				world.clients[2] = client
    107 				if scenario == "other owner" {
    108 					binary.BigEndian.PutUint32(request[8:], 2)
    109 				}
    110 			case "stale client":
    111 				world.clients[1] = &worldClient{id: 1}
    112 			case "loading":
    113 				client.loading = true
    114 			case "not ready":
    115 				client.ready = false
    116 			case "selector":
    117 				binary.BigEndian.PutUint32(request[20:], 1000001)
    118 			}
    119 			output.packets = nil
    120 			auditCheckpointWrites(t, world)
    121 
    122 			err := world.handlePetCommand(client, request)
    123 
    124 			if err != nil || len(world.characters[1].pets) != 1 || world.characters[1000001] == nil ||
    125 				len(world.petExpiry) != 1 || len(output.packets) != 0 || len(checkpointWrites(t, world)) != 0 {
    126 				t.Fatal("untrusted dismissal retired another owner's pet", err)
    127 			}
    128 		})
    129 	}
    130 }
    131 
    132 func TestPetDismissNotifiesAnObserverStillLoadingItsSnapshot(t *testing.T) {
    133 	world, _ := newPetSummonWorld(t)
    134 	client := world.clients[1]
    135 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    136 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    137 	observer := &combatTestConn{}
    138 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    139 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, target: true, ready: true, loading: true, conn: observer, npcs: map[uint32]bool{1000001: true}}
    140 
    141 	err := world.handlePetCommand(client, petDismissRequest(1)[20:])
    142 
    143 	if err != nil || len(observer.packets) != 3 || world.clients[2].npcs[1000001] ||
    144 		binary.BigEndian.Uint32(observer.packets[0][16:20]) != 0x58742a0f ||
    145 		binary.BigEndian.Uint32(observer.packets[2][16:20]) != 0x36510078 {
    146 		t.Fatal("queued pet snapshot did not receive retirement while loading", err, len(observer.packets))
    147 	}
    148 }
    149 
    150 func TestPetDismissInAnotherPlayfieldUpdatesItsOwnersLimit(t *testing.T) {
    151 	world, output := newPetSummonWorld(t)
    152 	client := world.clients[1]
    153 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    154 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    155 	world.characters[1].playfield = 951
    156 	clear(client.npcs)
    157 	output.packets = nil
    158 
    159 	err := world.handlePetCommand(client, petDismissRequest(1)[20:])
    160 
    161 	if err != nil || len(world.characters[1].pets) != 0 || world.characters[1000001] != nil ||
    162 		len(output.packets) != 2 || binary.BigEndian.Uint32(output.packets[1][29:33]) != 251 ||
    163 		binary.BigEndian.Uint32(output.packets[1][33:37]) != 0 {
    164 		t.Fatal("owner lost its limit correction after leaving the pet's playfield", err, len(output.packets))
    165 	}
    166 }
    167 
    168 func TestPetDismissAndExpiryRetireTheEntityOnce(t *testing.T) {
    169 	world, output := newPetSummonWorld(t)
    170 	client := world.clients[1]
    171 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    172 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    173 	deadline := world.characters[1000001].pet.expiresAt
    174 	output.packets = nil
    175 	auditCheckpointWrites(t, world)
    176 	start := make(chan struct{})
    177 	errors := make(chan error, 1)
    178 	zone, stop := startTestZone(t, world)
    179 	var workers sync.WaitGroup
    180 	workers.Add(2)
    181 
    182 	go func() {
    183 		defer workers.Done()
    184 		<-start
    185 		errors <- zone.call(zonePacket{client, petDismissRequest(1)[20:]})
    186 	}()
    187 	go func() {
    188 		defer workers.Done()
    189 		<-start
    190 		if err := zone.call(zoneTestCommand(func(state *server) error { state.advanceNanos(deadline); return nil })); err != nil {
    191 			t.Error(err)
    192 		}
    193 	}()
    194 	close(start)
    195 	workers.Wait()
    196 	if err := stop(); err != nil {
    197 		t.Fatal(err)
    198 	}
    199 
    200 	if err := <-errors; err != nil || world.characters[1000001] != nil || len(world.characters[1].pets) != 0 ||
    201 		len(world.petExpiry) != 0 || len(output.packets) != 3 || checkpointWrites(t, world)["characters/DELETE"] != 1 {
    202 		t.Fatal("concurrent expiry and dismissal did not publish one committed retirement", err, len(output.packets))
    203 	}
    204 }
    205 
    206 func TestPetDismissOldDeadlineCannotRetireReplacement(t *testing.T) {
    207 	world, _ := newPetSummonWorld(t)
    208 	player, client := world.characters[1], world.clients[1]
    209 	cast := characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:]
    210 	world.handleNanoCast(client, cast)
    211 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    212 	deadline := time.Now().Add(5 * time.Second)
    213 	world.characters[1000001].pet.expiresAt = deadline
    214 	if _, err := world.db.Exec("UPDATE pets SET expires_at=? WHERE npc_id=1000001", deadline); err != nil {
    215 		t.Fatal(err)
    216 	}
    217 
    218 	err := world.handlePetCommand(client, petDismissRequest(1)[20:])
    219 	player.nanoRechargeUntil = time.Time{}
    220 	world.handleNanoCast(client, cast)
    221 	if client.nanoCast == nil {
    222 		t.Fatal("retired owner limit did not admit its replacement")
    223 	}
    224 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
    225 	world.advanceNanos(deadline.Add(time.Nanosecond))
    226 	cold := newServerForTest(t)
    227 
    228 	if err != nil || world.characters[1000001] != nil || world.characters[1000002] == nil ||
    229 		len(world.petExpiry) != 1 || !player.pets[1000002] || player.nano != 15 ||
    230 		cold.characters[1000001] != nil || !cold.characters[1].pets[1000002] || cold.nextNPC != 1000003 {
    231 		t.Fatal("old deadline removed a replacement or reused its identity", err)
    232 	}
    233 }