Skip to content

Commit

Permalink
Make agent tests more robust (#632)
Browse files Browse the repository at this point in the history
* Make agent tests more robust
* Add String() to test logger
  • Loading branch information
yurishkuro committed Jan 3, 2018
1 parent c5ea85e commit b0d3fa9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
13 changes: 12 additions & 1 deletion cmd/agent/app/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"io"
"net"
"net/http"
"sync/atomic"

"go.uber.org/zap"

Expand All @@ -28,6 +29,7 @@ import (
type Agent struct {
processors []processors.Processor
httpServer *http.Server
httpAddr atomic.Value // string, set once agent starts listening
logger *zap.Logger
closer io.Closer
}
Expand All @@ -38,11 +40,13 @@ func NewAgent(
httpServer *http.Server,
logger *zap.Logger,
) *Agent {
return &Agent{
a := &Agent{
processors: processors,
httpServer: httpServer,
logger: logger,
}
a.httpAddr.Store("")
return a
}

// Run runs all of agent UDP and HTTP servers in separate go-routines.
Expand All @@ -53,18 +57,25 @@ func (a *Agent) Run() error {
if err != nil {
return err
}
a.httpAddr.Store(listener.Addr().String())
a.closer = listener
go func() {
if err := a.httpServer.Serve(listener); err != nil {
a.logger.Error("http server failure", zap.Error(err))
}
a.logger.Info("agent's http server exiting")
}()
for _, processor := range a.processors {
go processor.Serve()
}
return nil
}

// HTTPAddr returns the address that HTTP server is listening on
func (a *Agent) HTTPAddr() string {
return a.httpAddr.Load().(string)
}

// Stop forces all agent go routines to exit.
func (a *Agent) Stop() {
for _, processor := range a.processors {
Expand Down
26 changes: 24 additions & 2 deletions cmd/agent/app/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/pkg/testutils"
)

func TestAgentStartError(t *testing.T) {
Expand All @@ -45,8 +48,12 @@ func TestAgentStartStop(t *testing.T) {
},
},
},
HTTPServer: HTTPServerConfiguration{
HostPort: ":0",
},
}
agent, err := cfg.CreateAgent(zap.NewNop())
logger, logBuf := testutils.NewLogger()
agent, err := cfg.CreateAgent(logger)
require.NoError(t, err)
ch := make(chan error, 2)
go func() {
Expand All @@ -57,7 +64,14 @@ func TestAgentStartStop(t *testing.T) {
close(ch)
}()

url := fmt.Sprintf("http://%s/sampling?service=abc", agent.httpServer.Addr)
for i := 0; i < 1000; i++ {
if agent.HTTPAddr() != "" {
break
}
time.Sleep(time.Millisecond)
}

url := fmt.Sprintf("http://%s/sampling?service=abc", agent.HTTPAddr())
httpClient := &http.Client{
Timeout: 100 * time.Millisecond,
}
Expand Down Expand Up @@ -93,4 +107,12 @@ func TestAgentStartStop(t *testing.T) {

agent.Stop()
assert.NoError(t, <-ch)

for i := 0; i < 1000; i++ {
if strings.Contains(logBuf.String(), "agent's http server exiting") {
return
}
time.Sleep(time.Millisecond)
}
t.Fatal("Expecting server exist log")
}
7 changes: 7 additions & 0 deletions pkg/testutils/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ func (b *Buffer) Stripped() string {
return b.Buffer.Stripped()
}

// String overwrites zaptest.Buffer.String() to make it thread safe
func (b *Buffer) String() string {
b.RLock()
defer b.RUnlock()
return b.Buffer.String()
}

// Write overwrites zaptest.Buffer.bytes.Buffer.Write() to make it thread safe
func (b *Buffer) Write(p []byte) (int, error) {
b.Lock()
Expand Down
1 change: 1 addition & 0 deletions pkg/testutils/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestRaceCondition(t *testing.T) {
_ = <-start
buffer.Lines()
buffer.Stripped()
_ = buffer.String()
finish.Done()
}()

Expand Down

0 comments on commit b0d3fa9

Please sign in to comment.