Permalink
Browse files

Fix various linter errors (#583)

* gofmt fix
* gocyclo fixes
* Fixes for golint and ineffassign
* Add documentation for new FlattenTags method
* Fix test case assertion
  • Loading branch information...
isaachier authored and yurishkuro committed Dec 5, 2017
1 parent e86551d commit 1e10ffc0b51a73a6e31f96f5650f62a7fbf36297
@@ -38,6 +38,7 @@ func TestTBufferedReadTransport(t *testing.T) {
secondRead := make([]byte, 7)
n, err = trans.Read(secondRead)
require.NoError(t, err)
require.Equal(t, 6, n)
require.Equal(t, []byte("String"), secondRead[0:6])
require.Equal(t, uint64(0), trans.RemainingBytes())
@@ -192,7 +192,7 @@ func TestFlushErrors(t *testing.T) {
trans.Write([]byte{1, 2, 3, 4})
err = trans.Flush()
require.Error(t, trans.Flush(), "Flush with data should fail")
require.Error(t, err, "Flush with data should fail")
})
}
@@ -93,14 +93,14 @@ func TestGetSamplingRate(t *testing.T) {
// Test with no http server
agent := NewAgentService("", zap.NewNop())
rate, err := agent.GetSamplingRate("svc", "op")
_, err := agent.GetSamplingRate("svc", "op")
assert.Error(t, err)
agent = NewAgentService(server.URL, zap.NewNop())
rate, err = agent.GetSamplingRate("svc", "op")
rate, err := agent.GetSamplingRate("svc", "op")
assert.NoError(t, err)
assert.EqualValues(t, 1, rate)
rate, err = agent.GetSamplingRate("bad_svc", "op")
_, err = agent.GetSamplingRate("bad_svc", "op")
assert.Error(t, err)
}
@@ -15,8 +15,8 @@
package mocks
import (
"github.com/stretchr/testify/mock"
"github.com/crossdock/crossdock-go"
"github.com/stretchr/testify/mock"
)
// T is an autogenerated mock type for the T type
@@ -49,15 +49,15 @@ func TestGetTraces(t *testing.T) {
// Test with no http server
query := NewQueryService("", zap.NewNop())
traces, err := query.GetTraces("svc", "op", map[string]string{"key": "value"})
_, err := query.GetTraces("svc", "op", map[string]string{"key": "value"})
assert.Error(t, err)
query = NewQueryService(server.URL, zap.NewNop())
traces, err = query.GetTraces("svc", "op", map[string]string{"key": "value"})
traces, err := query.GetTraces("svc", "op", map[string]string{"key": "value"})
assert.NoError(t, err)
assert.Len(t, traces, 1)
assert.EqualValues(t, "traceid", traces[0].TraceID)
traces, err = query.GetTraces("bad_svc", "op", map[string]string{"key": "value"})
_, err = query.GetTraces("bad_svc", "op", map[string]string{"key": "value"})
assert.Error(t, err)
}
View
@@ -258,7 +258,7 @@ func (kvs KeyValues) FindByKey(key string) (KeyValue, bool) {
return KeyValue{}, false
}
// Equal compares KyValues with another list. Both lists must be already sorted.
// Equal compares KeyValues with another list. Both lists must be already sorted.
func (kvs KeyValues) Equal(other KeyValues) bool {
l1, l2 := len(kvs), len(other)
if l1 != l2 {
View
@@ -71,31 +71,34 @@ func TestCompareAndSwap(t *testing.T) {
assert.Equal(t, 1, cache.Size())
item, ok = cache.CompareAndSwap("B", nil, "Bar")
assert.True(t, ok)
assert.Equal(t, 2, cache.Size())
assert.Equal(t, "Bar", item)
assert.Equal(t, "Bar", cache.Get("B"))
item, ok = cache.CompareAndSwap("A", "Foo", "Foo2")
assert.Equal(t, true, ok)
assert.True(t, ok)
assert.Equal(t, "Foo2", item)
assert.Equal(t, "Foo2", cache.Get("A"))
item, ok = cache.CompareAndSwap("A", nil, "Foo3")
assert.Equal(t, false, ok)
assert.False(t, ok)
assert.Equal(t, "Foo2", item)
assert.Equal(t, "Foo2", cache.Get("A"))
item, ok = cache.CompareAndSwap("A", "Foo", "Foo3")
assert.False(t, ok)
assert.Equal(t, "Foo2", item)
assert.Equal(t, "Foo2", cache.Get("A"))
item, ok = cache.CompareAndSwap("F", "foo", "Foo3")
assert.Equal(t, false, ok)
assert.False(t, ok)
assert.Nil(t, item)
assert.Nil(t, cache.Get("F"))
// Evict the oldest entry
item, ok = cache.CompareAndSwap("E", nil, "Epsi")
assert.True(t, ok)
assert.Equal(t, "Epsi", item)
assert.Equal(t, "Foo2", cache.Get("A"))
assert.Nil(t, cache.Get("B")) // Oldest, should be evicted
@@ -22,5 +22,7 @@ type Lock interface {
// acquired is meaningless.
Acquire(resource string, ttl time.Duration) (acquired bool, err error)
// Forfeit forfeits a lease around a given resource. In case of an error,
// forfeited is meaningless.
Forfeit(resource string) (forfeited bool, err error)
}
View
@@ -180,7 +180,7 @@ type ESMultiSearchService struct {
multiSearchService *elastic.MultiSearchService
}
// WrapESSearchService creates an ESSearchService out of *elastic.ESSearchService.
// WrapESMultiSearchService creates an ESSearchService out of *elastic.ESSearchService.
func WrapESMultiSearchService(multiSearchService *elastic.MultiSearchService) ESMultiSearchService {
return ESMultiSearchService{multiSearchService: multiSearchService}
}
@@ -82,6 +82,7 @@ func TestPortBusy(t *testing.T) {
func TestHttpCall(t *testing.T) {
logger, _ := zap.NewDevelopment()
state, err := healthcheck.NewState(http.StatusServiceUnavailable, logger)
assert.NoError(t, err)
handler, err := healthcheck.NewHandler(state)
if err != nil {
t.Error("Could not start the health check server.", zap.Error(err))
@@ -100,15 +101,18 @@ func TestHttpCall(t *testing.T) {
func TestListenerClose(t *testing.T) {
logger, _ := zap.NewDevelopment()
state, err := healthcheck.NewState(http.StatusServiceUnavailable, logger)
assert.NoError(t, err)
handler, err := healthcheck.NewHandler(state)
if err != nil {
t.Error("Could not start the health check server.", zap.Error(err))
}
s := &http.Server{Handler: handler}
l, err := net.Listen("tcp", ":0")
assert.NoError(t, err)
defer l.Close()
s, err = healthcheck.ServeWithListener(l, s, logger)
_, err = healthcheck.ServeWithListener(l, s, logger)
assert.NoError(t, err)
}
func TestServeHandler(t *testing.T) {
@@ -180,6 +180,15 @@ func (m *Store) validTrace(trace *model.Trace, query *spanstore.TraceQueryParame
return false
}
func findKeyValueMatch(kvs model.KeyValues, key, value string) (model.KeyValue, bool) {
for _, kv := range kvs {
if kv.Key == key && kv.AsString() == value {
return kv, true
}
}
return model.KeyValue{}, false
}
func (m *Store) validSpan(span *model.Span, query *spanstore.TraceQueryParameters) bool {
if query.ServiceName != span.Process.ServiceName {
return false
@@ -201,22 +210,14 @@ func (m *Store) validSpan(span *model.Span, query *spanstore.TraceQueryParameter
}
spanKVs := m.flattenTags(span)
for queryK, queryV := range query.Tags {
keyValueFoundAndMatches := false
// (NB): we cannot find the KeyValue.Find function because there can be multiple tags with the same key
for _, keyValue := range spanKVs {
if keyValue.Key == queryK && keyValue.AsString() == queryV {
keyValueFoundAndMatches = true
break
}
}
if !keyValueFoundAndMatches {
// (NB): we cannot use the KeyValues.FindKey function because there can be multiple tags with the same key
if _, ok := findKeyValueMatch(spanKVs, queryK, queryV); !ok {
return false
}
}
return true
}
// TODO: this is a good candidate function to have on a span
func (m *Store) flattenTags(span *model.Span) model.KeyValues {
retMe := span.Tags
retMe = append(retMe, span.Process.Tags...)

0 comments on commit 1e10ffc

Please sign in to comment.