2018-05-07 19:03:40 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-23 08:05:56 +01:00
|
|
|
"runtime"
|
2018-05-07 19:03:40 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testMon = monkit.ScopeNamed("testpkg")
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMetrics(t *testing.T) {
|
2018-08-23 08:05:56 +01:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
//TODO (windows): currently closing doesn't seem to be shutting down the server
|
|
|
|
t.Skip("broken")
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:01:46 +01:00
|
|
|
s, err := Listen("127.0.0.1:0")
|
2018-05-07 19:03:40 +01:00
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
defer func() { _ = s.Close() }()
|
2018-05-07 19:03:40 +01:00
|
|
|
|
|
|
|
c, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "testapp",
|
|
|
|
Instance: "testinst",
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
testMon.IntVal("testint").Observe(3)
|
|
|
|
|
|
|
|
errs := make(chan error, 3)
|
|
|
|
go func() {
|
|
|
|
errs <- c.Report(context.Background())
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
errs <- s.Serve(context.Background(), HandlerFunc(
|
|
|
|
func(application, instance string, key []byte, val float64) {
|
|
|
|
assert.Equal(t, application, "testapp")
|
|
|
|
assert.Equal(t, instance, "testinst")
|
|
|
|
if string(key) == "testpkg.testint.recent" {
|
|
|
|
assert.Equal(t, val, float64(3))
|
|
|
|
errs <- nil
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}()
|
|
|
|
|
|
|
|
// three possible errors:
|
|
|
|
// * reporting will send an error or nil,
|
|
|
|
// * receiving will send an error or nil,
|
|
|
|
// * serving will return an error
|
|
|
|
// in the good case serving should return last and should return a closed
|
|
|
|
// error
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
err := <-errs
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, s.Close())
|
|
|
|
|
2018-05-07 19:03:40 +01:00
|
|
|
err = <-errs
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|