Skip to content

Commit

Permalink
test: rewrite tests with testify
Browse files Browse the repository at this point in the history
  • Loading branch information
torives authored and gligneul committed Nov 28, 2023
1 parent 35ee172 commit af328c6
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 102 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/supranational/blst v0.3.11 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ github.com/status-im/keycard-go v0.2.0 h1:QDLFswOQu1r5jsycloeQh3bVU8n/NatHHaZobt
github.com/status-im/keycard-go v0.2.0/go.mod h1:wlp8ZLbsmrF6g6WjugPAx+IzoLrkdf9+mHxBEeo3Hbg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
203 changes: 101 additions & 102 deletions internal/services/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,139 +5,138 @@ package services

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"time"

"github.com/cartesi/rollups-node/internal/logger"
"github.com/stretchr/testify/suite"
)

const (
servicePort = "44444"
serviceAdress = "0.0.0.0:" + servicePort
)

var tmpDir string
type ServiceTestSuite struct {
suite.Suite
tmpDir string
servicePort int
}

func setup() {
func (s *ServiceTestSuite) SetupSuite() {
logger.Init("warning", false)
buildFakeService()
s.buildFakeService()
s.servicePort = 55555
}

func (s *ServiceTestSuite) TearDownSuite() {
err := os.RemoveAll(s.tmpDir)
if err != nil {
panic(err)
}
}

func (s *ServiceTestSuite) SetupTest() {
s.servicePort++
serviceAdress := "0.0.0.0:" + fmt.Sprint(s.servicePort)
os.Setenv("SERVICE_ADDRESS", serviceAdress)
}

// Service should stop when context is cancelled
func (s *ServiceTestSuite) TestServiceStops() {
service := Service{
name: "fake-service",
binaryName: "fake-service",
healthcheckPort: fmt.Sprint(s.servicePort),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start service in goroutine
result := make(chan error)
go func() {
result <- service.Start(ctx)
}()

time.Sleep(100 * time.Millisecond)

// shutdown
cancel()
err := <-result
s.Nil(err, "service exited for the wrong reason: %v", err)
}

func tearDown() {
deleteTempDir()
// Service should stop if timeout is reached and it isn't ready yet
func (s *ServiceTestSuite) TestServiceTimeout() {
service := Service{
name: "fake-service",
binaryName: "fake-service",
healthcheckPort: "0000", // wrong port
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start service in goroutine
result := make(chan error, 1)
go func() {
result <- service.Start(ctx)
}()

// expect timeout because of wrong port
err := service.Ready(ctx, 500*time.Millisecond)
s.NotNil(err, "expected service to timeout")

// shutdown
cancel()
s.Nil(<-result, "service exited for the wrong reason: %v", err)
}

func TestService(t *testing.T) {
setup()

t.Run("it stops when the context is cancelled", func(t *testing.T) {
service := Service{
name: "fake-service",
binaryName: "fake-service",
healthcheckPort: servicePort,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start service in goroutine
result := make(chan error)
go func() {
result <- service.Start(ctx)
}()

time.Sleep(100 * time.Millisecond)

// shutdown
cancel()
if err := <-result; err != nil {
t.Errorf("service exited for the wrong reason: %v", err)
}
})

t.Run("it stops when timeout is reached and it isn't ready yet", func(t *testing.T) {
service := Service{
name: "fake-service",
binaryName: "fake-service",
healthcheckPort: "0000", // wrong port
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start service in goroutine
result := make(chan error, 1)
go func() {
result <- service.Start(ctx)
}()

// expect timeout because of wrong port
if err := service.Ready(ctx, 500*time.Millisecond); err == nil {
t.Errorf("expected service to timeout")
}

// shutdown
cancel()
if err := <-result; err != nil {
t.Errorf("service exited for the wrong reason: %v", err)
}
})

t.Run("it becomes ready soon after being started", func(t *testing.T) {
service := Service{
name: "fake-service",
binaryName: "fake-service",
healthcheckPort: servicePort,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start service in goroutine
result := make(chan error)
go func() {
result <- service.Start(ctx)
}()

// wait for service to be ready
if err := service.Ready(ctx, 500*time.Millisecond); err != nil {
t.Errorf("service timed out")
}

// shutdown
cancel()
if err := <-result; err != nil {
t.Errorf("service exited for the wrong reason: %v", err)
}
})

tearDown()
// Service should be ready soon after starting
func (s *ServiceTestSuite) TestServiceReady() {
service := Service{
name: "fake-service",
binaryName: "fake-service",
healthcheckPort: fmt.Sprint(s.servicePort),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// start service in goroutine
result := make(chan error)
go func() {
result <- service.Start(ctx)
}()

// wait for service to be ready
err := service.Ready(ctx, 1*time.Second)
s.Nil(err, "service timed out")

// shutdown
cancel()
s.Nil(<-result, "service exited for the wrong reason: %v", err)
}

// Builds the fake-service binary and adds it to PATH
func buildFakeService() {
temporaryDir, err := os.MkdirTemp("", "")
func (s *ServiceTestSuite) buildFakeService() {
tempDir, err := os.MkdirTemp("", "")
if err != nil {
panic(err)
}
tmpDir = temporaryDir
s.tmpDir = tempDir

cmd := exec.Command(
"go",
"build",
"-o",
filepath.Join(tmpDir, "fake-service"),
filepath.Join(s.tmpDir, "fake-service"),
"fakeservice/main.go",
)
if err := cmd.Run(); err != nil {
panic(err)
}
os.Setenv("PATH", os.Getenv("PATH")+":"+tmpDir)

os.Setenv("PATH", os.Getenv("PATH")+":"+s.tmpDir)
}

func deleteTempDir() {
err := os.RemoveAll(tmpDir)
if err != nil {
panic(err)
}
func TestServiceSuite(t *testing.T) {
suite.Run(t, new(ServiceTestSuite))
}

0 comments on commit af328c6

Please sign in to comment.