blob: 7cb54b66ad9b0509faab08933d2865b860c0891b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package net
// Close/broadcast race regression: a broadcast racing Session.Close() used
// to send on the outbound channel while Close closed the same channel — a
// genuine data race (closechan vs chansend) that the old panic-recover only
// hid. Closing is now signaled through a separate channel that is only ever
// closed, never sent to.
import (
stdnet "net"
"sync"
"testing"
"time"
)
// newTestSocketPair returns a synchronous, unbuffered connection pair: a
// write blocks until the peer reads, which is exactly the stall condition
// the echo test has to reproduce.
func newTestSocketPair() (stdnet.Conn, stdnet.Conn) { return stdnet.Pipe() }
func testTimeout() <-chan time.Time { return time.After(200 * time.Millisecond) }
func TestConcurrentWriteCloseIsRaceFree(t *testing.T) {
for i := 0; i < 50; i++ {
sess := &Session{Conn: &testConn{}}
sess.StartWriter()
var wg sync.WaitGroup
wg.Add(2)
start := make(chan struct{})
go func() {
defer wg.Done()
<-start
for j := 0; j < 100; j++ {
sess.WriteLine("broadcast")
}
}()
go func() {
defer wg.Done()
<-start
sess.Close()
}()
close(start)
wg.Wait()
<-sess.writeDone
}
}
// Echo negotiation regression: login flips telnet echo on the game owner's
// goroutine. Writing the negotiation inline made that write a direct socket
// I/O, so a client that never read (full receive window, stalled socket)
// blocked the entire game loop behind one connection. Queued sessions must
// route the negotiation through the outbound writer instead.
func TestSetEchoQueuedDoesNotBlockCaller(t *testing.T) {
local, peer := newTestSocketPair()
defer local.Close()
defer peer.Close()
sess := &Session{Conn: newTCPConn(local)}
sess.StartWriter()
defer sess.Close()
done := make(chan struct{})
go func() {
sess.SetEcho(false) // what the login path calls for password entry
close(done)
}()
select {
case <-done:
case <-testTimeout():
peer.Close() // unblock the writer so the test can exit
<-done
t.Fatal("SetEcho blocked the caller instead of queueing the negotiation")
}
}
|