aorecomp

Anarchy Online classic in the browser

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


server/combat_stress_test.go (9325B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"io"
      6 	"net"
      7 	"sync"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 type failingCombatConn struct {
     13 	combatTestConn
     14 	closed bool
     15 }
     16 
     17 func (conn *failingCombatConn) Write([]byte) (int, error) { return 0, io.ErrClosedPipe }
     18 func (conn *failingCombatConn) Close() error {
     19 	conn.closed = true
     20 	return nil
     21 }
     22 
     23 type stalledCombatConn struct {
     24 	net.Conn
     25 	started chan struct{}
     26 	once    sync.Once
     27 }
     28 
     29 func (conn *stalledCombatConn) Write(packet []byte) (int, error) {
     30 	conn.once.Do(func() { close(conn.started) })
     31 	return conn.Conn.Write(packet)
     32 }
     33 
     34 func TestCombatSharedNPCKillingBlowIsResolvedOnce(t *testing.T) {
     35 	world, first := newCombatWorld(t)
     36 	second := &combatTestConn{}
     37 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     38 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: second, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
     39 	world.clients[1].players = map[uint32]bool{2: true}
     40 	world.characters[1000000].health = 6
     41 	for _, id := range []uint32{1, 2} {
     42 		if err := world.handleCombat(world.clients[id], attackPacket(id, 1000000)[20:]); err != nil {
     43 			t.Fatal(err)
     44 		}
     45 	}
     46 	when := world.characters[2].nextAttack
     47 
     48 	world.advanceCombat(when)
     49 	world.advanceCombat(when.Add(time.Hour))
     50 
     51 	if world.characters[1000000].health != 0 || len(world.fighting) != 0 || world.characters[1].health != 100 || world.characters[2].health != 100 {
     52 		t.Fatal("shared NPC death left an attacker active or allowed retaliation")
     53 	}
     54 	for _, conn := range []*combatTestConn{first, second} {
     55 		var hits, deaths int
     56 		var killer uint32
     57 		for _, packet := range conn.packets {
     58 			switch binary.BigEndian.Uint32(packet[16:20]) {
     59 			case 0x46002f16:
     60 				hits++
     61 				if binary.BigEndian.Uint32(packet[49:53]) == 4 {
     62 					killer = binary.BigEndian.Uint32(packet[24:28])
     63 				}
     64 			case 0x5e477770:
     65 				deaths++
     66 			}
     67 		}
     68 		if hits != 2 || deaths != 1 || killer != 2 {
     69 			t.Fatalf("shared death announcements: hits=%d deaths=%d killer=%d", hits, deaths, killer)
     70 		}
     71 	}
     72 }
     73 
     74 func TestCombatSwitchingTargetsLeavesExistingRetaliation(t *testing.T) {
     75 	world, _ := newCombatWorld(t)
     76 	world.characters[1000001] = &character{id: 1000001, npc: true, appearance: 229, playfield: 950, position: [3]float32{2, 0, 0}, health: 100}
     77 	world.clients[1].npcs[1000001] = true
     78 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
     79 		t.Fatal(err)
     80 	}
     81 	first := world.characters[1].nextAttack
     82 	world.advanceCombat(first)
     83 	originalHealth := world.characters[1000000].health
     84 
     85 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000001)[20:]); err != nil {
     86 		t.Fatal(err)
     87 	}
     88 	world.advanceCombat(world.characters[1].nextAttack)
     89 
     90 	if world.characters[1].fighting != 1000001 || world.characters[1000000].health != originalHealth || world.characters[1000001].health < 95 || world.characters[1000001].health > 97 || world.characters[1].health != 95 || world.characters[1000000].fighting != 1 {
     91 		t.Fatal("target switch reused the old target or pacified its retaliation")
     92 	}
     93 }
     94 
     95 func TestCombatRepeatedStartStopDoesNotLeaveScheduledSwings(t *testing.T) {
     96 	world, _ := newCombatWorld(t)
     97 
     98 	for range 100 {
     99 		if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    100 			t.Fatal(err)
    101 		}
    102 		if err := world.handleCombat(world.clients[1], stopFightPacket(1)[20:]); err != nil {
    103 			t.Fatal(err)
    104 		}
    105 	}
    106 	world.advanceCombat(time.Now().Add(time.Hour))
    107 
    108 	if len(world.fighting) != 0 || !world.characters[1].nextAttack.IsZero() || world.characters[1].health != 100 || world.characters[1000000].health != 100 {
    109 		t.Fatal("start/stop requests retained a scheduled attack")
    110 	}
    111 }
    112 
    113 func TestCombatChangingTargetsCannotBypassRecharge(t *testing.T) {
    114 	for _, action := range []string{"switch target", "stop and restart"} {
    115 		t.Run(action, func(t *testing.T) {
    116 			world, _ := newCombatWorld(t)
    117 			world.characters[1000001] = &character{id: 1000001, npc: true, appearance: 229, playfield: 950, position: [3]float32{2, 0, 0}, health: 100}
    118 			world.clients[1].npcs[1000001] = true
    119 			if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    120 				t.Fatal(err)
    121 			}
    122 			world.advanceCombat(world.characters[1].nextAttack)
    123 			next := world.characters[1].nextAttack
    124 
    125 			target := uint32(1000001)
    126 			if action == "stop and restart" {
    127 				if err := world.handleCombat(world.clients[1], stopFightPacket(1)[20:]); err != nil {
    128 					t.Fatal(err)
    129 				}
    130 				target = 1000000
    131 			}
    132 			if err := world.handleCombat(world.clients[1], attackPacket(1, target)[20:]); err != nil {
    133 				t.Fatal(err)
    134 			}
    135 
    136 			if world.characters[1].nextAttack.Before(next) {
    137 				t.Fatalf("%s advanced the next attack from %s to %s", action, next, world.characters[1].nextAttack)
    138 			}
    139 		})
    140 	}
    141 }
    142 
    143 func TestCombatFailedObserverDoesNotSuppressOtherObservers(t *testing.T) {
    144 	world, healthy := newCombatWorld(t)
    145 	failed := &failingCombatConn{}
    146 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    147 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: failed, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    148 
    149 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    150 		t.Fatal(err)
    151 	}
    152 	world.advanceCombat(world.characters[1].nextAttack)
    153 
    154 	if !failed.closed || len(healthy.packets) != 3 || world.characters[1000000].health < 95 || world.characters[1000000].health > 97 {
    155 		t.Fatal("failed observer suppressed a healthy observer's combat updates")
    156 	}
    157 }
    158 
    159 func TestCombatLateObserverReceivesInjuredNPCBeforeFight(t *testing.T) {
    160 	world, _ := newCombatWorld(t)
    161 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    162 		t.Fatal(err)
    163 	}
    164 	world.advanceCombat(world.characters[1].nextAttack)
    165 	observer := &combatTestConn{}
    166 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    167 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: make(map[uint32]bool)}
    168 
    169 	world.advanceNPCs(time.Now())
    170 
    171 	if len(observer.packets) != 3 || binary.BigEndian.Uint32(observer.packets[0][16:20]) != 0x271b3a6b || binary.BigEndian.Uint32(observer.packets[0][83:87]) != world.characters[1000000].health {
    172 		t.Fatalf("late observer did not receive the injured NPC first: %x", observer.packets)
    173 	}
    174 	for _, packet := range observer.packets[1:] {
    175 		if binary.BigEndian.Uint32(packet[16:20]) != 0x28494070 {
    176 			t.Fatalf("late observer received historical damage instead of fighting targets: %x", packet)
    177 		}
    178 	}
    179 }
    180 
    181 func TestCombatStalledObserverCannotFreezeWorldUpdates(t *testing.T) {
    182 	world, _ := newCombatWorld(t)
    183 	peer, connection := net.Pipe()
    184 	if err := peer.SetReadDeadline(time.Now().Add(8 * time.Second)); err != nil {
    185 		t.Fatal(err)
    186 	}
    187 	stalled := &stalledCombatConn{Conn: connection, started: make(chan struct{})}
    188 	world.clients[1].conn = stalled
    189 	client := world.clients[1]
    190 	zone, stop := startTestZone(t, world)
    191 	attackDone, tickDone := make(chan struct{}), make(chan struct{})
    192 	t.Cleanup(func() {
    193 		peer.Close()
    194 		connection.Close()
    195 		<-attackDone
    196 		<-tickDone
    197 	})
    198 
    199 	go func() {
    200 		if err := zone.call(zonePacket{client, attackPacket(1, 1000000)[20:]}); err != nil {
    201 			t.Error(err)
    202 		}
    203 		close(attackDone)
    204 	}()
    205 	<-stalled.started
    206 	go func() {
    207 		if err := zone.call(zoneTestCommand(func(state *server) error { state.advanceNPCs(time.Now()); return nil })); err != nil {
    208 			t.Error(err)
    209 		}
    210 		close(tickDone)
    211 	}()
    212 
    213 	select {
    214 	case <-tickDone:
    215 	case <-time.After(6 * time.Second):
    216 		t.Fatal("a peer that stopped reading blocked unrelated world updates")
    217 	}
    218 	if err := stop(); err != nil {
    219 		t.Fatal(err)
    220 	}
    221 	if _, err := peer.Read(make([]byte, 1)); err != io.EOF {
    222 		t.Fatalf("stalled peer was not disconnected: %v", err)
    223 	}
    224 }
    225 
    226 func TestCombatConcurrentDisconnectAndTicksReleaseBothSides(t *testing.T) {
    227 	world, _ := newCombatWorld(t)
    228 	delete(world.clients, 1)
    229 	shutdown := startTestService(t, world)
    230 	client := connectZoningClient(t, world, false)
    231 	if _, err := client.Write(attackPacket(1, 1000000)); err != nil {
    232 		t.Fatal(err)
    233 	}
    234 	if _, err := readPacket(client); err != nil {
    235 		t.Fatal(err)
    236 	}
    237 	zone, err := world.world.owner(1)
    238 	if err != nil {
    239 		t.Fatal(err)
    240 	}
    241 	var first time.Time
    242 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error { first = state.characters[1].nextAttack; return nil }))
    243 	done := make(chan struct{})
    244 	t.Cleanup(func() {
    245 		client.Close()
    246 		<-done
    247 	})
    248 
    249 	go func() {
    250 		defer close(done)
    251 		for offset := range 100 {
    252 			if err := zone.call(zoneTestCommand(func(state *server) error {
    253 				state.advanceCombat(first.Add(time.Duration(offset) * time.Second))
    254 				return nil
    255 			})); err != nil {
    256 				t.Error(err)
    257 				return
    258 			}
    259 		}
    260 	}()
    261 	client.Close()
    262 	<-done
    263 	deadline := time.Now().Add(time.Second)
    264 	for {
    265 		var clean bool
    266 		if err := zone.call(zoneTestCommand(func(state *server) error {
    267 			clean = state.clients[1] == nil && len(state.fighting) == 0 && state.characters[1].fighting == 0 && state.characters[1000000].fighting == 0
    268 			return nil
    269 		})); err != nil {
    270 			t.Fatal(err)
    271 		}
    272 		if clean {
    273 			break
    274 		}
    275 		if time.Now().After(deadline) {
    276 			t.Fatal("disconnect racing combat ticks retained a connection or fighting target")
    277 		}
    278 		time.Sleep(time.Millisecond)
    279 	}
    280 	if err := shutdown(); err != nil {
    281 		t.Fatal(err)
    282 	}
    283 }