aorecomp

Anarchy Online classic in the browser

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


server/navigation_worker_test.go (7759B)


      1 package main
      2 
      3 import (
      4 	"testing"
      5 	"time"
      6 )
      7 
      8 func pauseNavigationForTest(t *testing.T, world *server, playfield uint32) {
      9 	t.Helper()
     10 	navigator := &zoneNavigator{jobs: make(chan *navigationJob, 32), stop: make(chan struct{}), done: make(chan struct{})}
     11 	world.navigation = map[uint32]*zoneNavigator{playfield: navigator}
     12 	resume := make(chan struct{})
     13 	world.navigationWorkers.Go(func() {
     14 		select {
     15 		case <-resume:
     16 		case <-navigator.stop:
     17 		}
     18 		navigator.run()
     19 	})
     20 	t.Cleanup(func() { close(resume); world.stopNavigation() })
     21 }
     22 
     23 func TestStalledZoneNavigationDoesNotBlockAnotherZone(t *testing.T) {
     24 	world, _, _ := enteredMissionEncounterWorld(t)
     25 	first, npc := world.characters[1], world.characters[2000003]
     26 	first.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
     27 	npc.fighting = first.id
     28 	npc.motion.moving = true
     29 	npc.motion.route = [][3]float32{{20, .011, 15}}
     30 	pauseNavigationForTest(t, world, first.playfield)
     31 	world.geometry.missions[65537] = world.geometry.mission(65536)
     32 	world.nextCharacter = max(world.nextCharacter, first.id)
     33 	second := world.newCharacter()
     34 	second.playfield, second.position, second.rotation = 65537, first.position, [4]float32{0, 0, 0, 1}
     35 	second.geometry = world.geometry
     36 	client := &worldClient{id: second.id, ready: true, target: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
     37 	world.clients[second.id] = client
     38 	other := *npc
     39 	other.id, other.playfield, other.fighting = 3000000, second.playfield, second.id
     40 	other.motion = &npcMotion{home: other.position}
     41 	world.addNPC(&other)
     42 	now := time.Now()
     43 	world.lastNPCUpdate = now
     44 
     45 	finished := make(chan error, 1)
     46 	go func() {
     47 		world.advanceNPCs(now.Add(250 * time.Millisecond))
     48 		finished <- world.handleMovement(client, playerMovementRequest(second, 1))
     49 	}()
     50 	select {
     51 	case err := <-finished:
     52 		if err != nil {
     53 			t.Fatal(err)
     54 		}
     55 	case <-time.After(time.Second):
     56 		t.Fatal("a stalled zone blocked another zone's command")
     57 	}
     58 	if npc.motion.navigation == nil || other.motion.navigation == nil {
     59 		t.Fatal("fixture did not queue both zone searches")
     60 	}
     61 	stalledAt := npc.position
     62 	select {
     63 	case <-other.motion.navigation.done:
     64 	case <-time.After(time.Second):
     65 		t.Fatal("another zone's navigation waited for the stalled worker")
     66 	}
     67 	world.advanceNPCs(now.Add(500 * time.Millisecond))
     68 	before := second.position
     69 	world.advancePlayerMovement(second.movement.updatedAt.Add(100 * time.Millisecond))
     70 
     71 	if npc.motion.moving || npc.position != stalledAt || npc.motion.navigation == nil {
     72 		t.Fatal("stalled search unexpectedly advanced its actor")
     73 	}
     74 	if !other.motion.moving || len(other.motion.route) == 0 || second.position == before {
     75 		t.Fatal("independent zone did not advance navigation and player movement", other.motion, before, second.position)
     76 	}
     77 }
     78 
     79 func TestNavigationDiscardsAResultForAnOldTarget(t *testing.T) {
     80 	world, _, _ := enteredMissionEncounterWorld(t)
     81 	player, npc := world.characters[1], world.characters[2000003]
     82 	player.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
     83 	npc.fighting = player.id
     84 	now := time.Now()
     85 	world.advanceNPCs(now)
     86 	job := npc.motion.navigation
     87 	if job == nil {
     88 		t.Fatal("fixture did not request a route")
     89 	}
     90 	select {
     91 	case <-job.done:
     92 	case <-time.After(time.Second):
     93 		t.Fatal("navigation worker did not finish")
     94 	}
     95 
     96 	other := *player
     97 	other.id, other.position = 2, [3]float32{15, .011, 43}
     98 	world.characters[other.id] = &other
     99 	world.clients[other.id] = &worldClient{id: other.id, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
    100 	npc.fighting = other.id
    101 	world.advanceNPCs(now.Add(250 * time.Millisecond))
    102 
    103 	if len(npc.motion.route) != 0 || npc.motion.navigation == nil || npc.motion.navigation == job {
    104 		t.Fatal("stale route was published or retained")
    105 	}
    106 	select {
    107 	case <-job.cancel:
    108 	default:
    109 		t.Fatal("replaced request was not cancelled")
    110 	}
    111 }
    112 
    113 func TestNavigationShutdownStopsQueuedWork(t *testing.T) {
    114 	world, _, _ := enteredMissionEncounterWorld(t)
    115 	player, npc := world.characters[1], world.characters[2000003]
    116 	player.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
    117 	npc.fighting = player.id
    118 	pauseNavigationForTest(t, world, player.playfield)
    119 	world.advanceNPCs(time.Now())
    120 	navigator := world.navigation[player.playfield]
    121 
    122 	finished := make(chan struct{})
    123 	go func() { world.stopNavigation(); close(finished) }()
    124 
    125 	select {
    126 	case <-finished:
    127 	case <-time.After(time.Second):
    128 		t.Fatal("shutdown waited for queued navigation work")
    129 	}
    130 	select {
    131 	case <-navigator.done:
    132 	default:
    133 		t.Fatal("shutdown retained a navigation worker")
    134 	}
    135 }
    136 
    137 func TestNavigationRootCancelsAnOutstandingSearch(t *testing.T) {
    138 	world, _, _ := enteredMissionEncounterWorld(t)
    139 	player, npc := world.characters[1], world.characters[2000003]
    140 	player.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
    141 	npc.fighting = player.id
    142 	pauseNavigationForTest(t, world, player.playfield)
    143 	now := time.Now()
    144 	world.advanceNPCs(now)
    145 	job := npc.motion.navigation
    146 	npc.activeNanos = []activeNano{{program: 42110, caster: player.id, expiresAt: now.Add(time.Minute)}}
    147 
    148 	world.advanceNPCs(now.Add(250 * time.Millisecond))
    149 
    150 	if npc.motion.navigation != nil || npc.motion.moving || npc.position != [3]float32{15, .011, 15} {
    151 		t.Fatal("root retained pending navigation or moved the NPC")
    152 	}
    153 	select {
    154 	case <-job.cancel:
    155 	default:
    156 		t.Fatal("root did not cancel the outstanding search")
    157 	}
    158 }
    159 
    160 func TestNavigationMissionRetirementStopsItsWorker(t *testing.T) {
    161 	world, _, _ := enteredMissionEncounterWorld(t)
    162 	player, npc := world.characters[1], world.characters[2000003]
    163 	player.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
    164 	npc.fighting = player.id
    165 	zone := player.playfield
    166 	pauseNavigationForTest(t, world, zone)
    167 	world.advanceNPCs(time.Now())
    168 	navigator := world.navigation[zone]
    169 
    170 	err := world.handleMissionRemoval(world.clients[1], missionRemovalPacket(1, 2)[20:])
    171 
    172 	if err != nil || world.navigation[zone] != nil || world.characters[npc.id] != nil {
    173 		t.Fatal("mission retirement retained its worker or NPC", err)
    174 	}
    175 	select {
    176 	case <-navigator.done:
    177 	case <-time.After(time.Second):
    178 		t.Fatal("retired mission's navigation worker did not stop")
    179 	}
    180 }
    181 
    182 func TestNavigationPursuesATargetMovingBetweenEveryTick(t *testing.T) {
    183 	for _, mode := range []string{"pursuit", "pet follow"} {
    184 		t.Run(mode, func(t *testing.T) {
    185 			world, _, _ := enteredMissionEncounterWorld(t)
    186 			player, npc := world.characters[1], world.characters[2000003]
    187 			player.position, npc.position = [3]float32{12.5, .011, 34}, [3]float32{15, .011, 15}
    188 			npc.fighting = player.id
    189 			now := time.Now()
    190 			world.lastNPCUpdate = now
    191 			advance := world.advanceNPCs
    192 			if mode == "pet follow" {
    193 				npc.pet = &ownedPet{owner: player.id}
    194 				npc.fighting, npc.followTarget, npc.running = 0, player.id, true
    195 				npc.geometry, npc.movement.updatedAt = world.geometry, now
    196 				world.petExpiry = []*character{npc}
    197 				player.pets = map[uint32]bool{npc.id: true}
    198 				advance = world.advancePlayerMovement
    199 			}
    200 
    201 			// Finish searches between ticks, but move the target before consuming each
    202 			// result. Repeated same-time ticks would conceal cancellation starvation.
    203 			for tick := 1; tick <= 8; tick++ {
    204 				player.position[0] = 12.5 + float32(tick%2)*2.5
    205 				advance(now.Add(time.Duration(tick) * 250 * time.Millisecond))
    206 				if job := npc.motion.navigation; job != nil {
    207 					select {
    208 					case <-job.done:
    209 					case <-time.After(time.Second):
    210 						t.Fatal("navigation worker did not finish between ticks")
    211 					}
    212 				}
    213 			}
    214 
    215 			if surfaceDistanceSquared(npc.position, [3]float32{15, .011, 15}) < 1 {
    216 				t.Fatal("moving target starved pursuit despite completed route searches", npc.position)
    217 			}
    218 		})
    219 	}
    220 }