reconnect_test.go raw

   1  package neutrino
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"io"
   7  	"net"
   8  	"testing"
   9  	"time"
  10  
  11  	"git.smesh.lol/nostrino/pkg/chain"
  12  	"git.smesh.lol/nostrino/pkg/p2p"
  13  )
  14  
  15  // TestASyncRedialsAfterTheStreamFails: a relay can go away between two syncs.
  16  // The client must not decide it is "already connected" because a dead peer is
  17  // still assigned; the next request has to dial again.
  18  func TestASyncRedialsAfterTheStreamFails(t *testing.T) {
  19  	profile, err := chain.ByName("regtest")
  20  
  21  	if err != nil {
  22  		t.Fatal(err)
  23  	}
  24  
  25  	dials := 0
  26  
  27  	client, err := New(Config{
  28  		Profile: profile,
  29  		Store:   NewMemory(),
  30  		Dial: func(context.Context) (net.Conn, error) {
  31  			dials++
  32  
  33  			return nil, errors.New("the relay is down")
  34  		},
  35  	})
  36  
  37  	if err != nil {
  38  		t.Fatal(err)
  39  	}
  40  
  41  	for want := 1; want <= 2; want++ {
  42  		if _, err := client.SyncHeaders(context.Background()); err == nil {
  43  			t.Fatalf("attempt %d: a sync with no reachable relay succeeded", want)
  44  		}
  45  
  46  		if dials != want {
  47  			t.Fatalf("after %d attempt(s) the client dialed %d time(s)", want, dials)
  48  		}
  49  	}
  50  }
  51  
  52  // TestADeadStreamDropsThePeer: the read that notices EOF is what forgets the
  53  // peer, so the next request takes the redial path.
  54  func TestADeadStreamDropsThePeer(t *testing.T) {
  55  	profile, err := chain.ByName("regtest")
  56  
  57  	if err != nil {
  58  		t.Fatal(err)
  59  	}
  60  
  61  	client, err := New(Config{
  62  		Profile: profile,
  63  		Store:   NewMemory(),
  64  		Dial:    func(context.Context) (net.Conn, error) { return nil, errors.New("no peer") },
  65  	})
  66  
  67  	if err != nil {
  68  		t.Fatal(err)
  69  	}
  70  
  71  	local, remote := net.Pipe()
  72  	defer remote.Close()
  73  
  74  	watched := &watchedConn{Conn: local}
  75  	watched.onError = func() { client.dropConnection(watched) }
  76  
  77  	client.mu.Lock()
  78  	client.conn = watched
  79  	client.peer = p2p.New(watched, p2p.Config{Magic: profile.Magic})
  80  	client.mu.Unlock()
  81  
  82  	// The far end going away is what an ended tunnel looks like to the reader.
  83  	remote.Close()
  84  
  85  	select {
  86  	case <-time.After(5 * time.Second):
  87  		t.Fatal("the dead stream was not noticed")
  88  	case <-closedSignal(func() {
  89  		buf := make([]byte, 8)
  90  		_, _ = watched.Read(buf)
  91  	}):
  92  	}
  93  
  94  	if client.peer != nil || client.conn != nil {
  95  		t.Fatalf("a dead stream was still assigned: peer=%v conn=%v", client.peer, client.conn)
  96  	}
  97  }
  98  
  99  // TestATimeoutKeepsThePeer: a command deadline is the caller giving up, not the
 100  // relay dying; dropping the peer there would reconnect on every short deadline.
 101  func TestATimeoutKeepsThePeer(t *testing.T) {
 102  	conn := &timeoutConn{}
 103  	watched := &watchedConn{Conn: conn}
 104  	dropped := false
 105  	watched.onError = func() { dropped = true }
 106  
 107  	buf := make([]byte, 1)
 108  
 109  	if _, err := watched.Read(buf); err == nil {
 110  		t.Fatal("the read was supposed to time out")
 111  	}
 112  
 113  	if dropped {
 114  		t.Fatal("a timeout was treated as a dead stream")
 115  	}
 116  
 117  	if _, err := watched.Read(buf); !errors.Is(err, io.EOF) {
 118  		t.Fatalf("read: %v", err)
 119  	}
 120  
 121  	if !dropped {
 122  		t.Fatal("EOF did not report the stream as dead")
 123  	}
 124  }
 125  
 126  var errTimeout = errors.New("i/o timeout")
 127  
 128  // timeoutConn fails once with a timeout, then once with EOF.
 129  type timeoutConn struct {
 130  	calls int
 131  }
 132  
 133  func (c *timeoutConn) Read([]byte) (int, error) {
 134  	c.calls++
 135  
 136  	if c.calls == 1 {
 137  		return 0, timeoutError{}
 138  	}
 139  
 140  	return 0, io.EOF
 141  }
 142  
 143  func (c *timeoutConn) Write(p []byte) (int, error) { return len(p), nil }
 144  func (c *timeoutConn) Close() error                { return nil }
 145  func (c *timeoutConn) LocalAddr() net.Addr         { return nil }
 146  func (c *timeoutConn) RemoteAddr() net.Addr        { return nil }
 147  
 148  func (c *timeoutConn) SetDeadline(time.Time) error      { return nil }
 149  func (c *timeoutConn) SetReadDeadline(time.Time) error  { return nil }
 150  func (c *timeoutConn) SetWriteDeadline(time.Time) error { return nil }
 151  
 152  // timeoutError is a net.Error whose Timeout is true.
 153  type timeoutError struct{}
 154  
 155  func (timeoutError) Error() string   { return errTimeout.Error() }
 156  func (timeoutError) Timeout() bool   { return true }
 157  func (timeoutError) Temporary() bool { return true }
 158  
 159  // closedSignal runs fn and closes the channel when it returns.
 160  func closedSignal(fn func()) <-chan struct{} {
 161  	done := make(chan struct{})
 162  
 163  	go func() {
 164  		defer close(done)
 165  
 166  		fn()
 167  	}()
 168  
 169  	return done
 170  }
 171