Skip to content

Commit

Permalink
feat: re-organize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
larryanz committed Feb 13, 2025
1 parent 7184ddf commit c24aa0d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 42 deletions.
26 changes: 17 additions & 9 deletions internal/testutil/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,44 @@ func TestNewServer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer srv.Close()
srv.Start()

conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
defer conn.Close()

t.Cleanup(func() {
conn.Close()
srv.Close()
})
}

func TestNewServerWithHost(t *testing.T) {
hosts := []string{
func TestNewServerWithAddress(t *testing.T) {
addresses := []string{
":8181",
"0.0.0.0:8181",
"127.0.0.1:8181",
"localhost:8181",
}

for _, h := range hosts {
t.Run(fmt.Sprintf("GIVEN host %s THEN succeed to init new server", h), func(t *testing.T) {
srv, err := NewServerWithHost(h)
for _, a := range addresses {
t.Run(fmt.Sprintf("GIVEN host %s THEN succeed to init new server", a), func(t *testing.T) {
srv, err := NewServerWithAddress(a)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
srv.Start()

conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
conn.Close()

t.Cleanup(func() {
conn.Close()
srv.Close()
})
})
}
}
Expand Down
88 changes: 55 additions & 33 deletions pubsub/pstest/fake_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,49 @@ import (
)

func TestNewServerWithPort(t *testing.T) {
// Allocate an available port to use with NewServerWithPort and then close it so it's available.
// Note: There is no guarantee that the port does not become used between closing
// the listener and creating the new server with NewServerWithPort, but the chances are
// very small.
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()

// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on
port := getFreePort(t)
srv := NewServerWithPort(port)

conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
defer srv.Close()
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
if err != nil {
t.Fatal(err)
}
defer conn.Close()

t.Cleanup(func() {
conn.Close()
srv.Close()
})
}

func TestNewServerWithCallback(t *testing.T) {
// Allocate an available port to use with NewServerWithPort and then close it so it's available.
// Note: There is no guarantee that the port does not become used between closing
// the listener and creating the new server with NewServerWithPort, but the chances are
// very small.
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
func TestNewServerWithAddress(t *testing.T) {
hosts := []string{
"",
"0.0.0.0",
"127.0.0.1",
"localhost",
}
for _, h := range hosts {
port := getFreePort(t)
address := fmt.Sprintf("%s:%d", h, port)
t.Run(fmt.Sprintf("Init new server succeed with address %s", address), func(t *testing.T) {
srv := NewServerWithAddress(address)

conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
conn.Close()
srv.Close()
})
})
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()

}

func TestNewServerWithCallback(t *testing.T) {
port := getFreePort(t)
additionalFake := struct {
iampb.UnimplementedIAMPolicyServer
}{}
Expand All @@ -90,20 +97,35 @@ func TestNewServerWithCallback(t *testing.T) {

// Pass a non 0 port to demonstrate we can pass a hardcoded port for the server to listen on
srv := NewServerWithCallback(fmt.Sprintf("localhost:%d", port), callback)
if err != nil {
t.Fatal(err)
}
defer srv.Close()

conn, err := grpc.NewClient(srv.Addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
t.Fatal(err)
}
defer conn.Close()

if !verifyCallback {
t.Fatal("callback was not invoked")
}

t.Cleanup(func() {
conn.Close()
srv.Close()
})
}

// getFreePort allocates an available port then close it so it's available.
// Note: There is no guarantee that the port does not become used between closing
// the listener and creating the new server with the invocation function, but
// the chances are very small.
func getFreePort(t *testing.T) int {
l, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()

return port
}

func TestTopics(t *testing.T) {
Expand Down

0 comments on commit c24aa0d

Please sign in to comment.