Browse Source

Adds tests for Unix sockets

As per #3115, adds simple Unix socket connect/accept deadline tests in
pretty much the same way as the TCP connect/accept deadline tests work.
pull/3121/head
Thane Thomson 6 years ago
parent
commit
5f93220c61
1 changed files with 88 additions and 0 deletions
  1. +88
    -0
      privval/socket_test.go

+ 88
- 0
privval/socket_test.go View File

@ -1,7 +1,9 @@
package privval
import (
"io/ioutil"
"net"
"os"
"testing"
"time"
@ -82,3 +84,89 @@ func TestTCPListenerConnDeadline(t *testing.T) {
close(readyc)
<-donec
}
// testUnixAddr will attempt to obtain a platform-independent temporary file
// name for a Unix socket
func testUnixAddr() (string, error) {
f, err := ioutil.TempFile("", "tendermint-privval-test")
if err != nil {
return "", err
}
addr := f.Name()
f.Close()
os.Remove(addr)
return addr, nil
}
func TestUnixListenerAcceptDeadline(t *testing.T) {
addr, err := testUnixAddr()
if err != nil {
t.Fatal(err)
}
ln, err := net.Listen("unix", addr)
if err != nil {
t.Fatal(err)
}
unixLn := NewUnixListener(ln)
UnixListenerAcceptDeadline(time.Millisecond)(unixLn)
UnixListenerConnDeadline(time.Second)(unixLn)
_, err = unixLn.Accept()
opErr, ok := err.(*net.OpError)
if !ok {
t.Fatalf("have %v, want *net.OpError", err)
}
if have, want := opErr.Op, "accept"; have != want {
t.Errorf("have %v, want %v", have, want)
}
}
func TestUnixListenerConnDeadline(t *testing.T) {
addr, err := testUnixAddr()
if err != nil {
t.Fatal(err)
}
ln, err := net.Listen("unix", addr)
if err != nil {
t.Fatal(err)
}
unixLn := NewUnixListener(ln)
UnixListenerAcceptDeadline(time.Second)(unixLn)
UnixListenerConnDeadline(time.Millisecond)(unixLn)
readyc := make(chan struct{})
donec := make(chan struct{})
go func(ln net.Listener) {
defer close(donec)
c, err := ln.Accept()
if err != nil {
t.Fatal(err)
}
<-readyc
time.Sleep(2 * time.Millisecond)
msg := make([]byte, 200)
_, err = c.Read(msg)
opErr, ok := err.(*net.OpError)
if !ok {
t.Fatalf("have %v, want *net.OpError", err)
}
if have, want := opErr.Op, "read"; have != want {
t.Errorf("have %v, want %v", have, want)
}
}(unixLn)
dialer := DialUnixFn(addr)
_, err = dialer()
if err != nil {
t.Fatal(err)
}
close(readyc)
<-donec
}

Loading…
Cancel
Save