mirror of https://github.com/tailscale/tailscale/
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
2 years ago
|
// Copyright (c) Tailscale Inc & AUTHORS
|
||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||
5 years ago
|
|
||
2 years ago
|
package memnet
|
||
5 years ago
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
4 years ago
|
"os"
|
||
5 years ago
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestPipeHello(t *testing.T) {
|
||
|
p := NewPipe("p1", 1<<16)
|
||
|
msg := "Hello, World!"
|
||
|
if n, err := p.Write([]byte(msg)); err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if n != len(msg) {
|
||
|
t.Errorf("p.Write(%q) n=%d, want %d", msg, n, len(msg))
|
||
|
}
|
||
|
b := make([]byte, len(msg))
|
||
|
if n, err := p.Read(b); err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if n != len(b) {
|
||
|
t.Errorf("p.Read(%q) n=%d, want %d", string(b[:n]), n, len(b))
|
||
|
}
|
||
|
if got := string(b); got != msg {
|
||
|
t.Errorf("p.Read: %q, want %q", got, msg)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestPipeTimeout(t *testing.T) {
|
||
|
t.Run("write", func(t *testing.T) {
|
||
|
p := NewPipe("p1", 1<<16)
|
||
|
p.SetWriteDeadline(time.Now().Add(-1 * time.Second))
|
||
|
n, err := p.Write([]byte{'h'})
|
||
4 years ago
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
||
5 years ago
|
t.Errorf("missing write timeout got err: %v", err)
|
||
|
}
|
||
|
if n != 0 {
|
||
|
t.Errorf("n=%d on timeout", n)
|
||
|
}
|
||
|
})
|
||
|
t.Run("read", func(t *testing.T) {
|
||
|
p := NewPipe("p1", 1<<16)
|
||
|
p.Write([]byte{'h'})
|
||
|
|
||
|
p.SetReadDeadline(time.Now().Add(-1 * time.Second))
|
||
|
b := make([]byte, 1)
|
||
|
n, err := p.Read(b)
|
||
4 years ago
|
if !errors.Is(err, os.ErrDeadlineExceeded) {
|
||
5 years ago
|
t.Errorf("missing read timeout got err: %v", err)
|
||
|
}
|
||
|
if n != 0 {
|
||
|
t.Errorf("n=%d on timeout", n)
|
||
|
}
|
||
|
})
|
||
|
t.Run("block-write", func(t *testing.T) {
|
||
|
p := NewPipe("p1", 1<<16)
|
||
|
p.SetWriteDeadline(time.Now().Add(10 * time.Millisecond))
|
||
|
if err := p.Block(); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
if _, err := p.Write([]byte{'h'}); !errors.Is(err, os.ErrDeadlineExceeded) {
|
||
5 years ago
|
t.Fatalf("want write timeout got: %v", err)
|
||
|
}
|
||
|
})
|
||
|
t.Run("block-read", func(t *testing.T) {
|
||
|
p := NewPipe("p1", 1<<16)
|
||
|
p.Write([]byte{'h', 'i'})
|
||
|
p.SetReadDeadline(time.Now().Add(10 * time.Millisecond))
|
||
|
b := make([]byte, 1)
|
||
|
if err := p.Block(); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
if _, err := p.Read(b); !errors.Is(err, os.ErrDeadlineExceeded) {
|
||
5 years ago
|
t.Fatalf("want read timeout got: %v", err)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestLimit(t *testing.T) {
|
||
|
p := NewPipe("p1", 1)
|
||
|
errCh := make(chan error)
|
||
|
go func() {
|
||
|
n, err := p.Write([]byte{'a', 'b', 'c'})
|
||
|
if err != nil {
|
||
|
errCh <- err
|
||
|
} else if n != 3 {
|
||
|
errCh <- fmt.Errorf("p.Write n=%d, want 3", n)
|
||
|
} else {
|
||
|
errCh <- nil
|
||
|
}
|
||
|
}()
|
||
|
b := make([]byte, 3)
|
||
|
|
||
|
if n, err := p.Read(b); err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if n != 1 {
|
||
|
t.Errorf("Read(%q): n=%d want 1", string(b), n)
|
||
|
}
|
||
|
if n, err := p.Read(b); err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if n != 1 {
|
||
|
t.Errorf("Read(%q): n=%d want 1", string(b), n)
|
||
|
}
|
||
|
if n, err := p.Read(b); err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if n != 1 {
|
||
|
t.Errorf("Read(%q): n=%d want 1", string(b), n)
|
||
|
}
|
||
4 years ago
|
|
||
|
if err := <-errCh; err != nil {
|
||
|
t.Error(err)
|
||
|
}
|
||
5 years ago
|
}
|