aorecomp

Anarchy Online classic in the browser

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


server/chat_output_test.go (2163B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestChatSlowRecipientDoesNotBlockVicinityDelivery(t *testing.T) {
     11 	world := chatTestServer(t)
     12 	alice := loginChatTest(t, world, 1)
     13 	bob := loginChatTest(t, world, 2)
     14 	done := make(chan struct{})
     15 	t.Cleanup(func() { alice.Close(); bob.Close(); <-done })
     16 
     17 	go func() { world.sendVicinity(world.clients[1], 0, "hello", nil); close(done) }()
     18 
     19 	select {
     20 	case <-done:
     21 	case <-time.After(250 * time.Millisecond):
     22 		t.Fatal("a recipient that stopped reading blocked vicinity delivery")
     23 	}
     24 	for _, expected := range []uint16{20, 34} {
     25 		kind, _, err := readChatPacket(alice)
     26 		if err != nil || kind != expected {
     27 			t.Fatalf("healthy recipient: kind=%d expected=%d error=%v", kind, expected, err)
     28 		}
     29 	}
     30 }
     31 
     32 func TestChatSlowRecipientDoesNotBlockGlobalDeliveryOrSender(t *testing.T) {
     33 	world := chatTestServer(t)
     34 	world.characters[3] = &character{id: 3, name: "Carol", playfield: 710}
     35 	world.clients[3] = &worldClient{id: 3, ready: true}
     36 	alice := loginChatTest(t, world, 1)
     37 	bob := loginChatTest(t, world, 2)
     38 	carol := loginChatTest(t, world, 3)
     39 	message := binary.BigEndian.AppendUint32([]byte{0x87}, 1)
     40 	message = appendChatString(appendChatString(message, "hello"), "")
     41 	ping := chatPacket(100, []byte{1, 2, 3})
     42 	done := make(chan error, 1)
     43 	t.Cleanup(func() { alice.Close(); bob.Close(); carol.Close() })
     44 
     45 	go func() {
     46 		_, err := alice.Write(append(chatPacket(65, message), ping...))
     47 		done <- err
     48 	}()
     49 
     50 	select {
     51 	case err := <-done:
     52 		if err != nil {
     53 			t.Fatal(err)
     54 		}
     55 	case <-time.After(250 * time.Millisecond):
     56 		t.Fatal("a slow recipient blocked the sender's next request")
     57 	}
     58 	for _, expected := range []uint16{20, 65} {
     59 		kind, _, err := readChatPacket(carol)
     60 		if err != nil || kind != expected {
     61 			t.Fatalf("healthy recipient: kind=%d expected=%d error=%v", kind, expected, err)
     62 		}
     63 	}
     64 	for _, expected := range []uint16{20, 65, 100} {
     65 		kind, body, err := readChatPacket(alice)
     66 		if err != nil || kind != expected || kind == 100 && !bytes.Equal(body, []byte{1, 2, 3}) {
     67 			t.Fatalf("sender response: kind=%d expected=%d body=%x error=%v", kind, expected, body, err)
     68 		}
     69 	}
     70 }