2016-05-06 16:44:32 +02:00
|
|
|
package transport
|
2016-03-02 17:33:42 +02:00
|
|
|
|
|
|
|
import (
|
2024-07-02 14:06:14 +02:00
|
|
|
"errors"
|
2016-05-10 12:23:30 +02:00
|
|
|
"io"
|
2019-07-17 09:38:50 +02:00
|
|
|
"net"
|
2021-12-05 13:56:17 +02:00
|
|
|
"sync"
|
2016-03-02 17:33:42 +02:00
|
|
|
"testing"
|
2016-07-29 16:30:51 +02:00
|
|
|
"time"
|
2016-03-02 17:33:42 +02:00
|
|
|
)
|
|
|
|
|
2016-05-06 16:44:32 +02:00
|
|
|
func expectedPort(t *testing.T, expected string, lsn Listener) {
|
2019-07-17 09:38:50 +02:00
|
|
|
_, port, err := net.SplitHostPort(lsn.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Expected address to be `%s`, got error: %v", expected, err)
|
|
|
|
}
|
2016-03-14 19:50:18 +02:00
|
|
|
|
|
|
|
if port != expected {
|
|
|
|
lsn.Close()
|
|
|
|
t.Errorf("Expected address to be `%s`, got `%s`", expected, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPTransportPortRange(t *testing.T) {
|
2020-12-29 17:49:26 +02:00
|
|
|
tp := NewHTTPTransport()
|
2016-03-02 17:33:42 +02:00
|
|
|
|
|
|
|
lsn1, err := tp.Listen(":44444-44448")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error, got %s", err)
|
|
|
|
}
|
|
|
|
expectedPort(t, "44444", lsn1)
|
|
|
|
|
|
|
|
lsn2, err := tp.Listen(":44444-44448")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error, got %s", err)
|
|
|
|
}
|
|
|
|
expectedPort(t, "44445", lsn2)
|
|
|
|
|
2017-11-03 19:30:16 +02:00
|
|
|
lsn, err := tp.Listen("127.0.0.1:0")
|
2016-03-02 17:33:42 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Did not expect an error, got %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lsn.Close()
|
|
|
|
lsn1.Close()
|
|
|
|
lsn2.Close()
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:50:18 +02:00
|
|
|
func TestHTTPTransportCommunication(t *testing.T) {
|
2020-12-29 17:49:26 +02:00
|
|
|
tr := NewHTTPTransport()
|
2016-03-02 17:33:42 +02:00
|
|
|
|
2017-11-03 19:30:16 +02:00
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
2016-03-14 19:50:18 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
2016-05-06 16:44:32 +02:00
|
|
|
fn := func(sock Socket) {
|
2016-03-14 20:20:38 +02:00
|
|
|
defer sock.Close()
|
|
|
|
|
2016-03-14 19:50:18 +02:00
|
|
|
for {
|
2016-05-06 16:44:32 +02:00
|
|
|
var m Message
|
2016-03-14 19:50:18 +02:00
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sock.Send(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 20:20:38 +02:00
|
|
|
done := make(chan bool)
|
|
|
|
|
2016-03-14 19:50:18 +02:00
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
2016-03-14 20:20:38 +02:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
2016-03-14 19:50:18 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2016-05-06 16:44:32 +02:00
|
|
|
m := Message{
|
2016-03-14 19:50:18 +02:00
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Errorf("Unexpected send err: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-05-06 16:44:32 +02:00
|
|
|
var rm Message
|
2016-03-14 19:50:18 +02:00
|
|
|
|
|
|
|
if err := c.Recv(&rm); err != nil {
|
|
|
|
t.Errorf("Unexpected recv err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(rm.Body) != string(m.Body) {
|
|
|
|
t.Errorf("Expected %v, got %v", m.Body, rm.Body)
|
2016-03-02 17:33:42 +02:00
|
|
|
}
|
2016-03-14 20:20:38 +02:00
|
|
|
|
|
|
|
close(done)
|
2016-03-02 17:33:42 +02:00
|
|
|
}
|
2016-05-06 16:44:32 +02:00
|
|
|
|
|
|
|
func TestHTTPTransportError(t *testing.T) {
|
2020-12-29 17:49:26 +02:00
|
|
|
tr := NewHTTPTransport()
|
2016-05-06 16:44:32 +02:00
|
|
|
|
2017-11-03 19:30:16 +02:00
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
2016-05-06 16:44:32 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
fn := func(sock Socket) {
|
|
|
|
defer sock.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m Message
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
2024-07-02 14:06:14 +02:00
|
|
|
if errors.Is(err, io.EOF) {
|
2016-05-10 12:23:30 +02:00
|
|
|
return
|
|
|
|
}
|
2016-05-06 16:44:32 +02:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sock.(*httpTransportSocket).error(&Message{
|
|
|
|
Body: []byte(`an error occurred`),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
m := Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Errorf("Unexpected send err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rm Message
|
|
|
|
|
|
|
|
err = c.Recv(&rm)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("Expected error but got nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err.Error() != "500 Internal Server Error: an error occurred" {
|
|
|
|
t.Fatalf("Did not receive expected error, got: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
close(done)
|
|
|
|
}
|
2016-07-29 16:30:51 +02:00
|
|
|
|
2016-08-01 17:31:27 +02:00
|
|
|
func TestHTTPTransportTimeout(t *testing.T) {
|
2020-12-29 17:49:26 +02:00
|
|
|
tr := NewHTTPTransport(Timeout(time.Millisecond * 100))
|
2016-07-29 16:30:51 +02:00
|
|
|
|
2017-11-03 19:30:16 +02:00
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
2016-07-29 16:30:51 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
fn := func(sock Socket) {
|
|
|
|
defer func() {
|
|
|
|
sock.Close()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("deadline not executed")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m Message
|
|
|
|
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
m := Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Errorf("Unexpected send err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|
2021-12-05 13:56:17 +02:00
|
|
|
|
|
|
|
func TestHTTPTransportCloseWhenRecv(t *testing.T) {
|
|
|
|
tr := NewHTTPTransport()
|
|
|
|
|
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
fn := func(sock Socket) {
|
|
|
|
defer sock.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m Message
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := sock.Send(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
m := Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
var rm Message
|
|
|
|
|
|
|
|
if err := c.Recv(&rm); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
for i := 1; i < 3; i++ {
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Errorf("Unexpected send err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2022-07-15 12:04:41 +02:00
|
|
|
|
|
|
|
func TestHTTPTransportMultipleSendWhenRecv(t *testing.T) {
|
|
|
|
tr := NewHTTPTransport()
|
|
|
|
|
|
|
|
l, err := tr.Listen("127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
readyToSend := make(chan struct{})
|
|
|
|
m := Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
2024-07-02 14:06:14 +02:00
|
|
|
var wgSend sync.WaitGroup
|
2022-07-15 12:04:41 +02:00
|
|
|
fn := func(sock Socket) {
|
|
|
|
defer sock.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var mr Message
|
|
|
|
if err := sock.Recv(&mr); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer wgSend.Done()
|
|
|
|
<-readyToSend
|
|
|
|
if err := sock.Send(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr(), WithStream())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
readyForRecv := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
close(readyForRecv)
|
|
|
|
for {
|
|
|
|
var rm Message
|
|
|
|
if err := c.Recv(&rm); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2024-07-02 14:06:14 +02:00
|
|
|
wgSend.Add(3)
|
2022-07-15 12:04:41 +02:00
|
|
|
<-readyForRecv
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Errorf("Unexpected send err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(readyToSend)
|
|
|
|
wgSend.Wait()
|
|
|
|
close(done)
|
|
|
|
|
|
|
|
c.Close()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2022-07-30 11:56:14 +02:00
|
|
|
|
|
|
|
func TestHttpTransportListenerNetListener(t *testing.T) {
|
|
|
|
address := "127.0.0.1:0"
|
|
|
|
|
|
|
|
customListener, err := net.Listen("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tr := NewHTTPTransport(Timeout(time.Millisecond * 100))
|
|
|
|
|
|
|
|
// injection
|
|
|
|
l, err := tr.Listen(address, NetListener(customListener))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected listen err: %v", err)
|
|
|
|
}
|
|
|
|
defer l.Close()
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
fn := func(sock Socket) {
|
|
|
|
defer func() {
|
|
|
|
sock.Close()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatal("deadline not executed")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
var m Message
|
|
|
|
|
|
|
|
if err := sock.Recv(&m); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
if err := l.Accept(fn); err != nil {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
default:
|
|
|
|
t.Errorf("Unexpected accept err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
c, err := tr.Dial(l.Addr())
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected dial err: %v", err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
m := Message{
|
|
|
|
Header: map[string]string{
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
Body: []byte(`{"message": "Hello World"}`),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Send(&m); err != nil {
|
|
|
|
t.Errorf("Unexpected send err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
}
|