[project] Remove over complicated packages, useless tests, dead functions (#2780)
* [project] Dead test * [node] If fields always same, then just use the constant * [project] Remove overcomplicated ctxerror * [project] Remove more dead tests, adjust error replacing ctxerror * [project] More dead tests, fix travis complaint on Errorf * [node.sh] Remove is-genesispull/2784/head
parent
7178eabbf1
commit
2836f0f620
@ -1,137 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: interfaces.go
|
|
||||||
|
|
||||||
// Package mock_rawdb is a generated GoMock package.
|
|
||||||
package mock_rawdb |
|
||||||
|
|
||||||
import ( |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockDatabaseReader is a mock of DatabaseReader interface
|
|
||||||
type MockDatabaseReader struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockDatabaseReaderMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockDatabaseReaderMockRecorder is the mock recorder for MockDatabaseReader
|
|
||||||
type MockDatabaseReaderMockRecorder struct { |
|
||||||
mock *MockDatabaseReader |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockDatabaseReader creates a new mock instance
|
|
||||||
func NewMockDatabaseReader(ctrl *gomock.Controller) *MockDatabaseReader { |
|
||||||
mock := &MockDatabaseReader{ctrl: ctrl} |
|
||||||
mock.recorder = &MockDatabaseReaderMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockDatabaseReader) EXPECT() *MockDatabaseReaderMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Has mocks base method
|
|
||||||
func (m *MockDatabaseReader) Has(key []byte) (bool, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Has", key) |
|
||||||
ret0, _ := ret[0].(bool) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// Has indicates an expected call of Has
|
|
||||||
func (mr *MockDatabaseReaderMockRecorder) Has(key interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Has", reflect.TypeOf((*MockDatabaseReader)(nil).Has), key) |
|
||||||
} |
|
||||||
|
|
||||||
// Get mocks base method
|
|
||||||
func (m *MockDatabaseReader) Get(key []byte) ([]byte, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Get", key) |
|
||||||
ret0, _ := ret[0].([]byte) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// Get indicates an expected call of Get
|
|
||||||
func (mr *MockDatabaseReaderMockRecorder) Get(key interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockDatabaseReader)(nil).Get), key) |
|
||||||
} |
|
||||||
|
|
||||||
// MockDatabaseWriter is a mock of DatabaseWriter interface
|
|
||||||
type MockDatabaseWriter struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockDatabaseWriterMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockDatabaseWriterMockRecorder is the mock recorder for MockDatabaseWriter
|
|
||||||
type MockDatabaseWriterMockRecorder struct { |
|
||||||
mock *MockDatabaseWriter |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockDatabaseWriter creates a new mock instance
|
|
||||||
func NewMockDatabaseWriter(ctrl *gomock.Controller) *MockDatabaseWriter { |
|
||||||
mock := &MockDatabaseWriter{ctrl: ctrl} |
|
||||||
mock.recorder = &MockDatabaseWriterMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockDatabaseWriter) EXPECT() *MockDatabaseWriterMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Put mocks base method
|
|
||||||
func (m *MockDatabaseWriter) Put(key, value []byte) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Put", key, value) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Put indicates an expected call of Put
|
|
||||||
func (mr *MockDatabaseWriterMockRecorder) Put(key, value interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Put", reflect.TypeOf((*MockDatabaseWriter)(nil).Put), key, value) |
|
||||||
} |
|
||||||
|
|
||||||
// MockDatabaseDeleter is a mock of DatabaseDeleter interface
|
|
||||||
type MockDatabaseDeleter struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockDatabaseDeleterMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockDatabaseDeleterMockRecorder is the mock recorder for MockDatabaseDeleter
|
|
||||||
type MockDatabaseDeleterMockRecorder struct { |
|
||||||
mock *MockDatabaseDeleter |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockDatabaseDeleter creates a new mock instance
|
|
||||||
func NewMockDatabaseDeleter(ctrl *gomock.Controller) *MockDatabaseDeleter { |
|
||||||
mock := &MockDatabaseDeleter{ctrl: ctrl} |
|
||||||
mock.recorder = &MockDatabaseDeleterMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockDatabaseDeleter) EXPECT() *MockDatabaseDeleterMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Delete mocks base method
|
|
||||||
func (m *MockDatabaseDeleter) Delete(key []byte) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Delete", key) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Delete indicates an expected call of Delete
|
|
||||||
func (mr *MockDatabaseDeleterMockRecorder) Delete(key interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockDatabaseDeleter)(nil).Delete), key) |
|
||||||
} |
|
@ -1,35 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"path" |
|
||||||
"strings" |
|
||||||
) |
|
||||||
|
|
||||||
// Path is a pathname matcher.
|
|
||||||
//
|
|
||||||
// A value matches if it is the same as the matcher pattern or has the matcher
|
|
||||||
// pattern as a trailing component. For example,
|
|
||||||
// a pattern "abc/def" matches "abc/def" itself, "omg/abc/def",
|
|
||||||
// but not "abc/def/wtf", "abc/omg/def", or "xabc/def".
|
|
||||||
//
|
|
||||||
// Both the pattern and the value are sanitized using path.Clean() before use.
|
|
||||||
//
|
|
||||||
// The empty pattern "" matches only the empty value "".
|
|
||||||
type Path string |
|
||||||
|
|
||||||
// Matches returns whether x is the matching pathname itself or ends with the
|
|
||||||
// matching pathname, inside another directory.
|
|
||||||
func (p Path) Matches(x interface{}) bool { |
|
||||||
if s, ok := x.(string); ok { |
|
||||||
p1 := path.Clean(string(p)) |
|
||||||
s = path.Clean(s) |
|
||||||
return s == p1 || strings.HasSuffix(s, "/"+p1) |
|
||||||
} |
|
||||||
return false |
|
||||||
} |
|
||||||
|
|
||||||
// String returns the string representation of this pathname matcher.
|
|
||||||
func (p Path) String() string { |
|
||||||
return fmt.Sprintf("<path suffix %#v>", path.Clean(string(p))) |
|
||||||
} |
|
@ -1,51 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import "testing" |
|
||||||
|
|
||||||
func TestPathMatcher_Matches(t *testing.T) { |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
p Path |
|
||||||
x interface{} |
|
||||||
want bool |
|
||||||
}{ |
|
||||||
{"EmptyMatchesEmpty", "", "", true}, |
|
||||||
{"EmptyDoesNotMatchNonEmpty", "", "a", false}, |
|
||||||
{"EmptyDoesNotMatchNonEmptyEvenWithTrailingSlash", "", "a/", false}, |
|
||||||
{"NonEmptyDoesNotMatchEmpty", "a", "", false}, |
|
||||||
{"ExactIsOK", "abc/def", "abc/def", true}, |
|
||||||
{"SuffixIsOK", "abc/def", "omg/abc/def", true}, |
|
||||||
{"SubstringIsNotOK", "abc/def", "omg/abc/def/wtf", false}, |
|
||||||
{"PrefixIsNotOK", "abc/def", "abc/def/wtf", false}, |
|
||||||
{"InterveningElementIsNotOK", "abc/def", "abc/bbq/def", false}, |
|
||||||
{"GeneralNonMatch", "abc/def", "omg/wtf", false}, |
|
||||||
{"UncleanPattern", "abc//def/", "abc/def", true}, |
|
||||||
{"UncleanArg", "abc/def", "abc//def", true}, |
|
||||||
{"NonStringArg", "a", 'a', false}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := tt.p.Matches(tt.x); got != tt.want { |
|
||||||
t.Errorf("Path.Matches() = %v, want %v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestPathMatcher_String(t *testing.T) { |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
p Path |
|
||||||
want string |
|
||||||
}{ |
|
||||||
{"General", "abc/def", "<path suffix \"abc/def\">"}, |
|
||||||
{"Unclean", "abc//def/", "<path suffix \"abc/def\">"}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := tt.p.String(); got != tt.want { |
|
||||||
t.Errorf("Path.String() = %v, want %v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
@ -1,39 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// Slice is a gomock matcher that matches elements of an array or
|
|
||||||
// slice against its own members at the corresponding positions.
|
|
||||||
// Each member item in a Slice may be a regular item or a gomock
|
|
||||||
// Matcher instance.
|
|
||||||
type Slice []interface{} |
|
||||||
|
|
||||||
// Matches returns whether x is a slice with matching elements.
|
|
||||||
func (sm Slice) Matches(x interface{}) bool { |
|
||||||
v := reflect.ValueOf(x) |
|
||||||
switch v.Kind() { |
|
||||||
case reflect.Slice, reflect.Array: // OK
|
|
||||||
default: |
|
||||||
return false |
|
||||||
} |
|
||||||
l := v.Len() |
|
||||||
if l != len(sm) { |
|
||||||
return false |
|
||||||
} |
|
||||||
for i, m := range sm { |
|
||||||
m1 := toMatcher(m) |
|
||||||
v1 := v.Index(i).Interface() |
|
||||||
if !m1.Matches(v1) { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
||||||
|
|
||||||
// String returns the string representation of this slice matcher.
|
|
||||||
func (sm Slice) String() string { |
|
||||||
return fmt.Sprint([]interface{}(sm)) |
|
||||||
} |
|
@ -1,114 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import ( |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/golang/mock/gomock" |
|
||||||
) |
|
||||||
|
|
||||||
func TestSliceMatcher_Matches(t *testing.T) { |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
sm Slice |
|
||||||
x interface{} |
|
||||||
want bool |
|
||||||
}{ |
|
||||||
{ |
|
||||||
"EmptyEqEmpty", |
|
||||||
Slice{}, |
|
||||||
[]interface{}{}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"EmptyNeNotEmpty", |
|
||||||
Slice{}, |
|
||||||
[]interface{}{1}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"NotEmptyNeEmpty", |
|
||||||
Slice{0}, |
|
||||||
[]interface{}{}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"CompareRawValuesUsingEqualityHappy", |
|
||||||
Slice{1, 2, 3}, |
|
||||||
[]interface{}{1, 2, 3}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"CompareRawValuesUsingEqualityUnhappy", |
|
||||||
Slice{1, 2, 3}, |
|
||||||
[]interface{}{1, 20, 3}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"CompareMatcherUsingItsMatchesHappy", |
|
||||||
Slice{gomock.Nil(), gomock.Eq(3)}, |
|
||||||
[]interface{}{nil, 3}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"CompareMatcherUsingItsMatchesUnhappy", |
|
||||||
Slice{gomock.Nil(), gomock.Eq(3)}, |
|
||||||
[]interface{}{0, 3}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"NestedHappy", |
|
||||||
Slice{Slice{3}, 30}, |
|
||||||
[]interface{}{[]interface{}{3}, 30}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"NestedUnhappy", |
|
||||||
Slice{Slice{3}, 30}, |
|
||||||
[]interface{}{[]interface{}{300}, 30}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"MatchSliceOfMoreSpecificTypes", |
|
||||||
Slice{1, 2, 3}, |
|
||||||
[]int{1, 2, 3}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"AcceptArraysToo", |
|
||||||
Slice{1, 2, 3}, |
|
||||||
[...]int{1, 2, 3}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"RejectString", |
|
||||||
Slice{'a', 'b', 'c'}, |
|
||||||
"abc", |
|
||||||
false, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := tt.sm.Matches(tt.x); got != tt.want { |
|
||||||
t.Errorf("Slice.Matches() = %v, want %v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestSliceMatcher_String(t *testing.T) { |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
sm Slice |
|
||||||
want string |
|
||||||
}{ |
|
||||||
{"int", []interface{}{3, 5, 7}, "[3 5 7]"}, |
|
||||||
{"string", []interface{}{"omg", "wtf", "bbq"}, "[omg wtf bbq]"}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := tt.sm.String(); got != tt.want { |
|
||||||
t.Errorf("Slice.String() = %v, want %v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
@ -1,53 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"reflect" |
|
||||||
"sort" |
|
||||||
"strings" |
|
||||||
) |
|
||||||
|
|
||||||
// Struct is a struct member matcher.
|
|
||||||
type Struct map[string]interface{} |
|
||||||
|
|
||||||
// Matches returns whether all specified members match.
|
|
||||||
func (sm Struct) Matches(x interface{}) bool { |
|
||||||
v := reflect.ValueOf(x) |
|
||||||
if v.Kind() == reflect.Ptr { |
|
||||||
v = v.Elem() |
|
||||||
} |
|
||||||
if v.Kind() != reflect.Struct { |
|
||||||
return false |
|
||||||
} |
|
||||||
for n, m := range sm { |
|
||||||
m1 := toMatcher(m) |
|
||||||
f := v.FieldByName(n) |
|
||||||
if f == (reflect.Value{}) { |
|
||||||
return false |
|
||||||
} |
|
||||||
f1 := f.Interface() |
|
||||||
if !m1.Matches(f1) { |
|
||||||
return false |
|
||||||
} |
|
||||||
} |
|
||||||
return true |
|
||||||
} |
|
||||||
|
|
||||||
func (sm Struct) String() string { |
|
||||||
var fields sort.StringSlice |
|
||||||
for name := range sm { |
|
||||||
fields = append(fields, name) |
|
||||||
} |
|
||||||
sort.Sort(fields) |
|
||||||
for i, name := range fields { |
|
||||||
value := sm[name] |
|
||||||
var vs string |
|
||||||
if _, ok := value.(fmt.Stringer); ok { |
|
||||||
vs = fmt.Sprintf("%s", value) |
|
||||||
} else { |
|
||||||
vs = fmt.Sprintf("%v", value) |
|
||||||
} |
|
||||||
fields[i] = fmt.Sprintf("%s=%s", name, vs) |
|
||||||
} |
|
||||||
return "<struct " + strings.Join(fields, " ") + ">" |
|
||||||
} |
|
@ -1,113 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import ( |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/golang/mock/gomock" |
|
||||||
) |
|
||||||
|
|
||||||
type stringable int |
|
||||||
|
|
||||||
func (s stringable) String() string { |
|
||||||
return "omg" |
|
||||||
} |
|
||||||
|
|
||||||
func TestStructMatcher_Matches(t *testing.T) { |
|
||||||
type value struct { |
|
||||||
A int |
|
||||||
B string |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
sm Struct |
|
||||||
x interface{} |
|
||||||
want bool |
|
||||||
}{ |
|
||||||
{ |
|
||||||
"EmptyMatchesEmpty", |
|
||||||
Struct{}, |
|
||||||
value{}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"EmptyMatchesAny", |
|
||||||
Struct{}, |
|
||||||
value{A: 3, B: "omg"}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"EmptyStillDoesNotMatchNonStruct", |
|
||||||
Struct{}, |
|
||||||
0, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"RegularFieldValuesUseEq1", |
|
||||||
Struct{"A": 3, "B": "omg"}, |
|
||||||
value{A: 3, B: "omg"}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"RegularFieldValuesUseEq2", |
|
||||||
Struct{"A": 3, "B": "omg"}, |
|
||||||
value{A: 4, B: "omg"}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"MatchersAreUsedVerbatim1", |
|
||||||
Struct{"A": gomock.Not(3), "B": gomock.Eq("omg")}, |
|
||||||
value{A: 4, B: "omg"}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"MatchersAreUsedVerbatim2", |
|
||||||
Struct{"A": gomock.Not(3), "B": gomock.Eq("omg")}, |
|
||||||
value{A: 3, B: "omg"}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"UnspecifiedFieldsAreIgnored", |
|
||||||
Struct{"A": 3}, |
|
||||||
value{A: 3, B: "omg"}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"MissingFieldsReturnFailure", |
|
||||||
Struct{"NOTFOUND": 3}, |
|
||||||
value{A: 3}, |
|
||||||
false, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"DerefsPointer", |
|
||||||
Struct{"A": 3}, |
|
||||||
&value{A: 3}, |
|
||||||
true, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := tt.sm.Matches(tt.x); got != tt.want { |
|
||||||
t.Errorf("Struct.Matches() = %v, want %v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestStructMatcher_String(t *testing.T) { |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
sm Struct |
|
||||||
want string |
|
||||||
}{ |
|
||||||
{"UsesStringer", Struct{"A": stringable(0)}, "<struct A=omg>"}, |
|
||||||
{"ReprIfNotStringable", Struct{"A": nil}, "<struct A=<nil>>"}, |
|
||||||
{"SortsByKey", Struct{"B": 3, "A": 4}, "<struct A=4 B=3>"}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := tt.sm.String(); got != tt.want { |
|
||||||
t.Errorf("Struct.String() = %v, want %v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
package matchers |
|
||||||
|
|
||||||
import ( |
|
||||||
"github.com/golang/mock/gomock" |
|
||||||
) |
|
||||||
|
|
||||||
func toMatcher(v interface{}) gomock.Matcher { |
|
||||||
if m, ok := v.(gomock.Matcher); ok { |
|
||||||
return m |
|
||||||
} |
|
||||||
return gomock.Eq(v) |
|
||||||
} |
|
@ -1,341 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: shardingconfig.go
|
|
||||||
|
|
||||||
// Package mock_shardingconfig is a generated GoMock package.
|
|
||||||
package mock_shardingconfig |
|
||||||
|
|
||||||
import ( |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
sharding "github.com/harmony-one/harmony/internal/configs/sharding" |
|
||||||
genesis "github.com/harmony-one/harmony/internal/genesis" |
|
||||||
numeric "github.com/harmony-one/harmony/numeric" |
|
||||||
big "math/big" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockSchedule is a mock of Schedule interface
|
|
||||||
type MockSchedule struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockScheduleMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockScheduleMockRecorder is the mock recorder for MockSchedule
|
|
||||||
type MockScheduleMockRecorder struct { |
|
||||||
mock *MockSchedule |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockSchedule creates a new mock instance
|
|
||||||
func NewMockSchedule(ctrl *gomock.Controller) *MockSchedule { |
|
||||||
mock := &MockSchedule{ctrl: ctrl} |
|
||||||
mock.recorder = &MockScheduleMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockSchedule) EXPECT() *MockScheduleMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// InstanceForEpoch mocks base method
|
|
||||||
func (m *MockSchedule) InstanceForEpoch(epoch *big.Int) sharding.Instance { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "InstanceForEpoch", epoch) |
|
||||||
ret0, _ := ret[0].(sharding.Instance) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// InstanceForEpoch indicates an expected call of InstanceForEpoch
|
|
||||||
func (mr *MockScheduleMockRecorder) InstanceForEpoch(epoch interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InstanceForEpoch", reflect.TypeOf((*MockSchedule)(nil).InstanceForEpoch), epoch) |
|
||||||
} |
|
||||||
|
|
||||||
// BlocksPerEpoch mocks base method
|
|
||||||
func (m *MockSchedule) BlocksPerEpoch() uint64 { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "BlocksPerEpoch") |
|
||||||
ret0, _ := ret[0].(uint64) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// BlocksPerEpoch indicates an expected call of BlocksPerEpoch
|
|
||||||
func (mr *MockScheduleMockRecorder) BlocksPerEpoch() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BlocksPerEpoch", reflect.TypeOf((*MockSchedule)(nil).BlocksPerEpoch)) |
|
||||||
} |
|
||||||
|
|
||||||
// CalcEpochNumber mocks base method
|
|
||||||
func (m *MockSchedule) CalcEpochNumber(blockNum uint64) *big.Int { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "CalcEpochNumber", blockNum) |
|
||||||
ret0, _ := ret[0].(*big.Int) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// CalcEpochNumber indicates an expected call of CalcEpochNumber
|
|
||||||
func (mr *MockScheduleMockRecorder) CalcEpochNumber(blockNum interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CalcEpochNumber", reflect.TypeOf((*MockSchedule)(nil).CalcEpochNumber), blockNum) |
|
||||||
} |
|
||||||
|
|
||||||
// IsLastBlock mocks base method
|
|
||||||
func (m *MockSchedule) IsLastBlock(blockNum uint64) bool { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "IsLastBlock", blockNum) |
|
||||||
ret0, _ := ret[0].(bool) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// IsLastBlock indicates an expected call of IsLastBlock
|
|
||||||
func (mr *MockScheduleMockRecorder) IsLastBlock(blockNum interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "IsLastBlock", reflect.TypeOf((*MockSchedule)(nil).IsLastBlock), blockNum) |
|
||||||
} |
|
||||||
|
|
||||||
// EpochLastBlock mocks base method
|
|
||||||
func (m *MockSchedule) EpochLastBlock(epochNum uint64) uint64 { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "EpochLastBlock", epochNum) |
|
||||||
ret0, _ := ret[0].(uint64) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// EpochLastBlock indicates an expected call of EpochLastBlock
|
|
||||||
func (mr *MockScheduleMockRecorder) EpochLastBlock(epochNum interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EpochLastBlock", reflect.TypeOf((*MockSchedule)(nil).EpochLastBlock), epochNum) |
|
||||||
} |
|
||||||
|
|
||||||
// VdfDifficulty mocks base method
|
|
||||||
func (m *MockSchedule) VdfDifficulty() int { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "VdfDifficulty") |
|
||||||
ret0, _ := ret[0].(int) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// VdfDifficulty indicates an expected call of VdfDifficulty
|
|
||||||
func (mr *MockScheduleMockRecorder) VdfDifficulty() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "VdfDifficulty", reflect.TypeOf((*MockSchedule)(nil).VdfDifficulty)) |
|
||||||
} |
|
||||||
|
|
||||||
// ConsensusRatio mocks base method
|
|
||||||
func (m *MockSchedule) ConsensusRatio() float64 { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "ConsensusRatio") |
|
||||||
ret0, _ := ret[0].(float64) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// ConsensusRatio indicates an expected call of ConsensusRatio
|
|
||||||
func (mr *MockScheduleMockRecorder) ConsensusRatio() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConsensusRatio", reflect.TypeOf((*MockSchedule)(nil).ConsensusRatio)) |
|
||||||
} |
|
||||||
|
|
||||||
// RandomnessStartingEpoch mocks base method
|
|
||||||
func (m *MockSchedule) RandomnessStartingEpoch() uint64 { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "RandomnessStartingEpoch") |
|
||||||
ret0, _ := ret[0].(uint64) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// RandomnessStartingEpoch indicates an expected call of RandomnessStartingEpoch
|
|
||||||
func (mr *MockScheduleMockRecorder) RandomnessStartingEpoch() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RandomnessStartingEpoch", reflect.TypeOf((*MockSchedule)(nil).RandomnessStartingEpoch)) |
|
||||||
} |
|
||||||
|
|
||||||
// GetNetworkID mocks base method
|
|
||||||
func (m *MockSchedule) GetNetworkID() sharding.NetworkID { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetNetworkID") |
|
||||||
ret0, _ := ret[0].(sharding.NetworkID) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetNetworkID indicates an expected call of GetNetworkID
|
|
||||||
func (mr *MockScheduleMockRecorder) GetNetworkID() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetNetworkID", reflect.TypeOf((*MockSchedule)(nil).GetNetworkID)) |
|
||||||
} |
|
||||||
|
|
||||||
// GetShardingStructure mocks base method
|
|
||||||
func (m *MockSchedule) GetShardingStructure(arg0, arg1 int) []map[string]interface{} { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetShardingStructure", arg0, arg1) |
|
||||||
ret0, _ := ret[0].([]map[string]interface{}) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetShardingStructure indicates an expected call of GetShardingStructure
|
|
||||||
func (mr *MockScheduleMockRecorder) GetShardingStructure(arg0, arg1 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetShardingStructure", reflect.TypeOf((*MockSchedule)(nil).GetShardingStructure), arg0, arg1) |
|
||||||
} |
|
||||||
|
|
||||||
// MockInstance is a mock of Instance interface
|
|
||||||
type MockInstance struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockInstanceMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockInstanceMockRecorder is the mock recorder for MockInstance
|
|
||||||
type MockInstanceMockRecorder struct { |
|
||||||
mock *MockInstance |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockInstance creates a new mock instance
|
|
||||||
func NewMockInstance(ctrl *gomock.Controller) *MockInstance { |
|
||||||
mock := &MockInstance{ctrl: ctrl} |
|
||||||
mock.recorder = &MockInstanceMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockInstance) EXPECT() *MockInstanceMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// NumShards mocks base method
|
|
||||||
func (m *MockInstance) NumShards() uint32 { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "NumShards") |
|
||||||
ret0, _ := ret[0].(uint32) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// NumShards indicates an expected call of NumShards
|
|
||||||
func (mr *MockInstanceMockRecorder) NumShards() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NumShards", reflect.TypeOf((*MockInstance)(nil).NumShards)) |
|
||||||
} |
|
||||||
|
|
||||||
// NumNodesPerShard mocks base method
|
|
||||||
func (m *MockInstance) NumNodesPerShard() int { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "NumNodesPerShard") |
|
||||||
ret0, _ := ret[0].(int) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// NumNodesPerShard indicates an expected call of NumNodesPerShard
|
|
||||||
func (mr *MockInstanceMockRecorder) NumNodesPerShard() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NumNodesPerShard", reflect.TypeOf((*MockInstance)(nil).NumNodesPerShard)) |
|
||||||
} |
|
||||||
|
|
||||||
// NumHarmonyOperatedNodesPerShard mocks base method
|
|
||||||
func (m *MockInstance) NumHarmonyOperatedNodesPerShard() int { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "NumHarmonyOperatedNodesPerShard") |
|
||||||
ret0, _ := ret[0].(int) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// NumHarmonyOperatedNodesPerShard indicates an expected call of NumHarmonyOperatedNodesPerShard
|
|
||||||
func (mr *MockInstanceMockRecorder) NumHarmonyOperatedNodesPerShard() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NumHarmonyOperatedNodesPerShard", reflect.TypeOf((*MockInstance)(nil).NumHarmonyOperatedNodesPerShard)) |
|
||||||
} |
|
||||||
|
|
||||||
// HarmonyVotePercent mocks base method
|
|
||||||
func (m *MockInstance) HarmonyVotePercent() numeric.Dec { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "HarmonyVotePercent") |
|
||||||
ret0, _ := ret[0].(numeric.Dec) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// HarmonyVotePercent indicates an expected call of HarmonyVotePercent
|
|
||||||
func (mr *MockInstanceMockRecorder) HarmonyVotePercent() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HarmonyVotePercent", reflect.TypeOf((*MockInstance)(nil).HarmonyVotePercent)) |
|
||||||
} |
|
||||||
|
|
||||||
// ExternalVotePercent mocks base method
|
|
||||||
func (m *MockInstance) ExternalVotePercent() numeric.Dec { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "ExternalVotePercent") |
|
||||||
ret0, _ := ret[0].(numeric.Dec) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// ExternalVotePercent indicates an expected call of ExternalVotePercent
|
|
||||||
func (mr *MockInstanceMockRecorder) ExternalVotePercent() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExternalVotePercent", reflect.TypeOf((*MockInstance)(nil).ExternalVotePercent)) |
|
||||||
} |
|
||||||
|
|
||||||
// HmyAccounts mocks base method
|
|
||||||
func (m *MockInstance) HmyAccounts() []genesis.DeployAccount { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "HmyAccounts") |
|
||||||
ret0, _ := ret[0].([]genesis.DeployAccount) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// HmyAccounts indicates an expected call of HmyAccounts
|
|
||||||
func (mr *MockInstanceMockRecorder) HmyAccounts() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HmyAccounts", reflect.TypeOf((*MockInstance)(nil).HmyAccounts)) |
|
||||||
} |
|
||||||
|
|
||||||
// FnAccounts mocks base method
|
|
||||||
func (m *MockInstance) FnAccounts() []genesis.DeployAccount { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "FnAccounts") |
|
||||||
ret0, _ := ret[0].([]genesis.DeployAccount) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// FnAccounts indicates an expected call of FnAccounts
|
|
||||||
func (mr *MockInstanceMockRecorder) FnAccounts() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FnAccounts", reflect.TypeOf((*MockInstance)(nil).FnAccounts)) |
|
||||||
} |
|
||||||
|
|
||||||
// FindAccount mocks base method
|
|
||||||
func (m *MockInstance) FindAccount(blsPubKey string) (bool, *genesis.DeployAccount) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "FindAccount", blsPubKey) |
|
||||||
ret0, _ := ret[0].(bool) |
|
||||||
ret1, _ := ret[1].(*genesis.DeployAccount) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// FindAccount indicates an expected call of FindAccount
|
|
||||||
func (mr *MockInstanceMockRecorder) FindAccount(blsPubKey interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAccount", reflect.TypeOf((*MockInstance)(nil).FindAccount), blsPubKey) |
|
||||||
} |
|
||||||
|
|
||||||
// ReshardingEpoch mocks base method
|
|
||||||
func (m *MockInstance) ReshardingEpoch() []*big.Int { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "ReshardingEpoch") |
|
||||||
ret0, _ := ret[0].([]*big.Int) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// ReshardingEpoch indicates an expected call of ReshardingEpoch
|
|
||||||
func (mr *MockInstanceMockRecorder) ReshardingEpoch() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReshardingEpoch", reflect.TypeOf((*MockInstance)(nil).ReshardingEpoch)) |
|
||||||
} |
|
||||||
|
|
||||||
// BlocksPerEpoch mocks base method
|
|
||||||
func (m *MockInstance) BlocksPerEpoch() uint64 { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "BlocksPerEpoch") |
|
||||||
ret0, _ := ret[0].(uint64) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// BlocksPerEpoch indicates an expected call of BlocksPerEpoch
|
|
||||||
func (mr *MockInstanceMockRecorder) BlocksPerEpoch() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BlocksPerEpoch", reflect.TypeOf((*MockInstance)(nil).BlocksPerEpoch)) |
|
||||||
} |
|
@ -1,163 +0,0 @@ |
|||||||
// Package ctxerror provides a context-aware error facility.
|
|
||||||
//
|
|
||||||
// Inspired by log15-style (semi-)structured logging,
|
|
||||||
// it also provides a log15 bridge.
|
|
||||||
package ctxerror |
|
||||||
|
|
||||||
//go:generate mockgen -source ctxerror.go -destination mock/ctxerror.go
|
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log" |
|
||||||
) |
|
||||||
|
|
||||||
// CtxError is a context-aware error container.
|
|
||||||
type CtxError interface { |
|
||||||
// Error returns a fully formatted message, with context info.
|
|
||||||
Error() string |
|
||||||
|
|
||||||
// Message returns the bare error message, without context info.
|
|
||||||
Message() string |
|
||||||
|
|
||||||
// Contexts returns message contexts.
|
|
||||||
// Caller shall not modify the returned map.
|
|
||||||
Contexts() map[string]interface{} |
|
||||||
|
|
||||||
// WithCause chains an error after the receiver.
|
|
||||||
// It returns the merged/chained instance,
|
|
||||||
// where the message is "<receiver.Message>: <c.Message>",
|
|
||||||
// and with contexts merged (ones in c takes precedence).
|
|
||||||
WithCause(c error) CtxError |
|
||||||
} |
|
||||||
|
|
||||||
type ctxError struct { |
|
||||||
msg string |
|
||||||
ctx map[string]interface{} |
|
||||||
} |
|
||||||
|
|
||||||
// New creates and returns a new context-aware error.
|
|
||||||
func New(msg string, ctx ...interface{}) CtxError { |
|
||||||
e := &ctxError{msg: msg, ctx: make(map[string]interface{})} |
|
||||||
e.updateCtx(ctx...) |
|
||||||
return e |
|
||||||
} |
|
||||||
|
|
||||||
func (e *ctxError) updateCtx(ctx ...interface{}) { |
|
||||||
var name string |
|
||||||
if len(ctx)%2 == 1 { |
|
||||||
ctx = append(ctx, nil) |
|
||||||
} |
|
||||||
for idx, value := range ctx { |
|
||||||
if idx%2 == 0 { |
|
||||||
name = value.(string) |
|
||||||
} else { |
|
||||||
e.ctx[name] = value |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// Error returns a fully formatted message, with context info.
|
|
||||||
func (e *ctxError) Error() string { |
|
||||||
s := e.msg |
|
||||||
for k, v := range e.ctx { |
|
||||||
s += fmt.Sprintf(", %s=%#v", k, v) |
|
||||||
} |
|
||||||
return s |
|
||||||
} |
|
||||||
|
|
||||||
// Message returns the bare error message, without context info.
|
|
||||||
func (e *ctxError) Message() string { |
|
||||||
return e.msg |
|
||||||
} |
|
||||||
|
|
||||||
// Contexts returns message contexts.
|
|
||||||
// Caller shall not modify the returned map.
|
|
||||||
func (e *ctxError) Contexts() map[string]interface{} { |
|
||||||
return e.ctx |
|
||||||
} |
|
||||||
|
|
||||||
// WithCause chains an error after the receiver.
|
|
||||||
// It returns the merged/chained instance,
|
|
||||||
// where the message is “<receiver.Message>: <c.Message>”,
|
|
||||||
// and with contexts merged (ones in c takes precedence).
|
|
||||||
func (e *ctxError) WithCause(c error) CtxError { |
|
||||||
r := &ctxError{msg: e.msg + ": ", ctx: make(map[string]interface{})} |
|
||||||
for k, v := range e.ctx { |
|
||||||
r.ctx[k] = v |
|
||||||
} |
|
||||||
switch c := c.(type) { |
|
||||||
case *ctxError: |
|
||||||
r.msg += c.msg |
|
||||||
for k, v := range c.ctx { |
|
||||||
r.ctx[k] = v |
|
||||||
} |
|
||||||
default: |
|
||||||
r.msg += c.Error() |
|
||||||
} |
|
||||||
return r |
|
||||||
} |
|
||||||
|
|
||||||
// Log15Func is a log15-compatible logging function.
|
|
||||||
type Log15Func func(msg string, ctx ...interface{}) |
|
||||||
|
|
||||||
// Log15Logger logs something with a log15-style logging function.
|
|
||||||
type Log15Logger interface { |
|
||||||
Log15(f Log15Func) |
|
||||||
} |
|
||||||
|
|
||||||
// Log15 logs the receiver with a log15-style logging function.
|
|
||||||
func (e *ctxError) Log15(f Log15Func) { |
|
||||||
var ctx []interface{} |
|
||||||
for k, v := range e.ctx { |
|
||||||
ctx = append(ctx, k, v) |
|
||||||
} |
|
||||||
f(e.msg, ctx...) |
|
||||||
} |
|
||||||
|
|
||||||
// Log15 logs an error with a log15-style logging function.
|
|
||||||
// It handles both regular errors and Log15Logger-compliant errors.
|
|
||||||
func Log15(f Log15Func, e error) { |
|
||||||
if e15, ok := e.(Log15Logger); ok { |
|
||||||
e15.Log15(f) |
|
||||||
} else { |
|
||||||
f(e.Error()) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// Log15WithMsg logs an error with a message prefix using a log15-style
|
|
||||||
// logging function. It is a shortcut for a common pattern of prepending a
|
|
||||||
// context prefix.
|
|
||||||
func Log15WithMsg(f Log15Func, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15(f, New(msg, ctx...).WithCause(e)) |
|
||||||
} |
|
||||||
|
|
||||||
// Trace logs an error with a message prefix using a log15-style logger.
|
|
||||||
func Trace(l log.Logger, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15WithMsg(l.Trace, e, msg, ctx...) |
|
||||||
} |
|
||||||
|
|
||||||
// Debug logs an error with a message prefix using a log15-style logger.
|
|
||||||
func Debug(l log.Logger, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15WithMsg(l.Debug, e, msg, ctx...) |
|
||||||
} |
|
||||||
|
|
||||||
// Info logs an error with a message prefix using a log15-style logger.
|
|
||||||
func Info(l log.Logger, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15WithMsg(l.Info, e, msg, ctx...) |
|
||||||
} |
|
||||||
|
|
||||||
// Warn logs an error with a message prefix using a log15-style logger.
|
|
||||||
func Warn(l log.Logger, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15WithMsg(l.Warn, e, msg, ctx...) |
|
||||||
} |
|
||||||
|
|
||||||
// Error logs an error with a message prefix using a log15-style logger.
|
|
||||||
func Error(l log.Logger, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15WithMsg(l.Error, e, msg, ctx...) |
|
||||||
} |
|
||||||
|
|
||||||
// Crit logs an error with a message prefix using a log15-style logger.
|
|
||||||
func Crit(l log.Logger, e error, msg string, ctx ...interface{}) { |
|
||||||
Log15WithMsg(l.Crit, e, msg, ctx...) |
|
||||||
} |
|
@ -1,363 +0,0 @@ |
|||||||
package ctxerror |
|
||||||
|
|
||||||
import ( |
|
||||||
"errors" |
|
||||||
"reflect" |
|
||||||
"testing" |
|
||||||
) |
|
||||||
|
|
||||||
func TestNew(t *testing.T) { |
|
||||||
type args struct { |
|
||||||
msg string |
|
||||||
ctx []interface{} |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
args args |
|
||||||
want CtxError |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "Empty", |
|
||||||
args: args{msg: "", ctx: []interface{}{}}, |
|
||||||
want: &ctxError{msg: "", ctx: map[string]interface{}{}}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
args: args{msg: "omg", ctx: []interface{}{"wtf", 1, "bbq", 2}}, |
|
||||||
want: &ctxError{msg: "omg", ctx: map[string]interface{}{"wtf": 1, "bbq": 2}}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Truncated", |
|
||||||
args: args{ |
|
||||||
msg: "omg", |
|
||||||
ctx: []interface{}{"wtf", 1, "bbq" /* missing value... */}, |
|
||||||
}, |
|
||||||
want: &ctxError{ |
|
||||||
msg: "omg", |
|
||||||
ctx: map[string]interface{}{"wtf": 1, "bbq": /* becomes */ nil}, |
|
||||||
}, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
if got := New(tt.args.msg, tt.args.ctx...); !reflect.DeepEqual(got, tt.want) { |
|
||||||
t.Errorf("New() = %#v, want %#v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func Test_ctxError_updateCtx(t *testing.T) { |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
before, after map[string]interface{} |
|
||||||
delta []interface{} |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "Empty", |
|
||||||
before: map[string]interface{}{"omg": 1, "wtf": 2, "bbq": 3}, |
|
||||||
delta: []interface{}{}, |
|
||||||
after: map[string]interface{}{"omg": 1, "wtf": 2, "bbq": 3}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
before: map[string]interface{}{"omg": 1, "wtf": 2, "bbq": 3}, |
|
||||||
delta: []interface{}{"omg", 10, "wtf", 20}, |
|
||||||
after: map[string]interface{}{"omg": 10, "wtf": 20, "bbq": 3}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Truncated", |
|
||||||
before: map[string]interface{}{"omg": 1, "wtf": 2, "bbq": 3}, |
|
||||||
delta: []interface{}{"omg", 10, "wtf" /* missing value... */}, |
|
||||||
after: map[string]interface{}{"omg": 10, "wtf": /* becomes */ nil, "bbq": 3}, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
e := &ctxError{msg: tt.name, ctx: tt.before} |
|
||||||
e.updateCtx(tt.delta...) |
|
||||||
if !reflect.DeepEqual(e.ctx, tt.after) { |
|
||||||
t.Errorf("expected ctx %#v != %#v seen", tt.after, e.ctx) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func Test_ctxError_Error(t *testing.T) { |
|
||||||
type fields struct { |
|
||||||
msg string |
|
||||||
ctx map[string]interface{} |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
fields fields |
|
||||||
want string |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "AllEmpty", |
|
||||||
fields: fields{msg: "", ctx: map[string]interface{}{}}, |
|
||||||
want: "", |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "CtxEmpty", |
|
||||||
fields: fields{msg: "omg", ctx: map[string]interface{}{}}, |
|
||||||
want: "omg", |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "MsgEmpty", |
|
||||||
fields: fields{msg: "", ctx: map[string]interface{}{"wtf": "bbq"}}, |
|
||||||
want: ", wtf=\"bbq\"", |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
fields: fields{msg: "omg", ctx: map[string]interface{}{"wtf": "bbq"}}, |
|
||||||
want: "omg, wtf=\"bbq\"", |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
e := &ctxError{ |
|
||||||
msg: tt.fields.msg, |
|
||||||
ctx: tt.fields.ctx, |
|
||||||
} |
|
||||||
if got := e.Error(); got != tt.want { |
|
||||||
t.Errorf("Error() = %#v, want %#v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func Test_ctxError_Message(t *testing.T) { |
|
||||||
type fields struct { |
|
||||||
msg string |
|
||||||
ctx map[string]interface{} |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
fields fields |
|
||||||
want string |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "AllEmpty", |
|
||||||
fields: fields{msg: "", ctx: map[string]interface{}{}}, |
|
||||||
want: "", |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "CtxEmpty", |
|
||||||
fields: fields{msg: "omg", ctx: map[string]interface{}{}}, |
|
||||||
want: "omg", |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "MsgEmpty", |
|
||||||
fields: fields{msg: "", ctx: map[string]interface{}{"wtf": "bbq"}}, |
|
||||||
want: "", |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
fields: fields{msg: "omg", ctx: map[string]interface{}{"wtf": "bbq"}}, |
|
||||||
want: "omg", |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
e := &ctxError{ |
|
||||||
msg: tt.fields.msg, |
|
||||||
ctx: tt.fields.ctx, |
|
||||||
} |
|
||||||
if got := e.Message(); got != tt.want { |
|
||||||
t.Errorf("Message() = %#v, want %#v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func Test_ctxError_Contexts(t *testing.T) { |
|
||||||
type fields struct { |
|
||||||
msg string |
|
||||||
ctx map[string]interface{} |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
fields fields |
|
||||||
want map[string]interface{} |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "Empty", |
|
||||||
fields: fields{msg: "", ctx: map[string]interface{}{}}, |
|
||||||
want: map[string]interface{}{}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
fields: fields{ |
|
||||||
msg: "", |
|
||||||
ctx: map[string]interface{}{"omg": 1, "wtf": 2, "bbq": 3}, |
|
||||||
}, |
|
||||||
want: map[string]interface{}{"omg": 1, "wtf": 2, "bbq": 3}, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
e := &ctxError{ |
|
||||||
msg: tt.fields.msg, |
|
||||||
ctx: tt.fields.ctx, |
|
||||||
} |
|
||||||
if got := e.Contexts(); !reflect.DeepEqual(got, tt.want) { |
|
||||||
t.Errorf("Contexts() = %#v, want %#v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func Test_ctxError_WithCause(t *testing.T) { |
|
||||||
type fields struct { |
|
||||||
msg string |
|
||||||
ctx map[string]interface{} |
|
||||||
} |
|
||||||
type args struct { |
|
||||||
c error |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
fields fields |
|
||||||
args args |
|
||||||
want CtxError |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "CtxError", |
|
||||||
fields: fields{ |
|
||||||
msg: "hello", |
|
||||||
ctx: map[string]interface{}{"omg": 1, "wtf": 2}, |
|
||||||
}, |
|
||||||
args: args{c: &ctxError{ |
|
||||||
msg: "world", |
|
||||||
ctx: map[string]interface{}{"wtf": 20, "bbq": 30}, |
|
||||||
}}, |
|
||||||
want: &ctxError{ |
|
||||||
msg: "hello: world", |
|
||||||
ctx: map[string]interface{}{"omg": 1, "wtf": 20, "bbq": 30}, |
|
||||||
}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "RegularError", |
|
||||||
fields: fields{ |
|
||||||
msg: "hello", |
|
||||||
ctx: map[string]interface{}{"omg": 1, "wtf": 2}, |
|
||||||
}, |
|
||||||
args: args{c: errors.New("world")}, |
|
||||||
want: &ctxError{ |
|
||||||
msg: "hello: world", |
|
||||||
ctx: map[string]interface{}{"omg": 1, "wtf": 2}, |
|
||||||
}, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
e := &ctxError{ |
|
||||||
msg: tt.fields.msg, |
|
||||||
ctx: tt.fields.ctx, |
|
||||||
} |
|
||||||
if got := e.WithCause(tt.args.c); !reflect.DeepEqual(got, tt.want) { |
|
||||||
t.Errorf("WithCause() = %#v, want %#v", got, tt.want) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func Test_ctxError_Log15(t *testing.T) { |
|
||||||
type fields struct { |
|
||||||
msg string |
|
||||||
ctx map[string]interface{} |
|
||||||
} |
|
||||||
type want struct { |
|
||||||
msg string |
|
||||||
ctx []interface{} |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
fields fields |
|
||||||
want want |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "Empty", |
|
||||||
fields: fields{msg: "", ctx: map[string]interface{}{}}, |
|
||||||
want: want{msg: "", ctx: nil}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
fields: fields{msg: "hello", ctx: map[string]interface{}{"omg": 1}}, |
|
||||||
want: want{msg: "hello", ctx: []interface{}{"omg", 1}}, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
called := false |
|
||||||
f := func(msg string, ctx ...interface{}) { |
|
||||||
called = true |
|
||||||
if msg != tt.want.msg { |
|
||||||
t.Errorf("expected message %#v != %#v seen", |
|
||||||
tt.want.msg, msg) |
|
||||||
} |
|
||||||
if !reflect.DeepEqual(ctx, tt.want.ctx) { |
|
||||||
t.Errorf("expected ctx %#v != %#v seen", ctx, tt.want.ctx) |
|
||||||
} |
|
||||||
} |
|
||||||
e := &ctxError{ |
|
||||||
msg: tt.fields.msg, |
|
||||||
ctx: tt.fields.ctx, |
|
||||||
} |
|
||||||
e.Log15(f) |
|
||||||
if !called { |
|
||||||
t.Error("logging func not called") |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestLog15(t *testing.T) { |
|
||||||
type args struct { |
|
||||||
e error |
|
||||||
} |
|
||||||
type want struct { |
|
||||||
msg string |
|
||||||
ctx []interface{} |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
args args |
|
||||||
want want |
|
||||||
}{ |
|
||||||
{ |
|
||||||
name: "Regular", |
|
||||||
args: args{e: errors.New("hello")}, |
|
||||||
want: want{msg: "hello", ctx: nil}, |
|
||||||
}, |
|
||||||
{ |
|
||||||
name: "CtxError", |
|
||||||
args: args{e: &ctxError{ |
|
||||||
msg: "hello", |
|
||||||
ctx: map[string]interface{}{"omg": 1}, |
|
||||||
}}, |
|
||||||
want: want{msg: "hello", ctx: []interface{}{"omg", 1}}, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
called := false |
|
||||||
f := func(msg string, ctx ...interface{}) { |
|
||||||
called = true |
|
||||||
if msg != tt.want.msg { |
|
||||||
t.Errorf("expected message %#v != %#v seen", |
|
||||||
tt.want.msg, msg) |
|
||||||
} |
|
||||||
if !reflect.DeepEqual(ctx, tt.want.ctx) { |
|
||||||
t.Errorf("expected ctx %#v != %#v seen", |
|
||||||
tt.want.ctx, ctx) |
|
||||||
} |
|
||||||
} |
|
||||||
Log15(f, tt.args.e) |
|
||||||
if !called { |
|
||||||
t.Errorf("logging func not called") |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
@ -1,125 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: ctxerror.go
|
|
||||||
|
|
||||||
// Package mock_ctxerror is a generated GoMock package.
|
|
||||||
package mock_ctxerror |
|
||||||
|
|
||||||
import ( |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
ctxerror "github.com/harmony-one/harmony/internal/ctxerror" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockCtxError is a mock of CtxError interface
|
|
||||||
type MockCtxError struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockCtxErrorMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockCtxErrorMockRecorder is the mock recorder for MockCtxError
|
|
||||||
type MockCtxErrorMockRecorder struct { |
|
||||||
mock *MockCtxError |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockCtxError creates a new mock instance
|
|
||||||
func NewMockCtxError(ctrl *gomock.Controller) *MockCtxError { |
|
||||||
mock := &MockCtxError{ctrl: ctrl} |
|
||||||
mock.recorder = &MockCtxErrorMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockCtxError) EXPECT() *MockCtxErrorMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Error mocks base method
|
|
||||||
func (m *MockCtxError) Error() string { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Error") |
|
||||||
ret0, _ := ret[0].(string) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Error indicates an expected call of Error
|
|
||||||
func (mr *MockCtxErrorMockRecorder) Error() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockCtxError)(nil).Error)) |
|
||||||
} |
|
||||||
|
|
||||||
// Message mocks base method
|
|
||||||
func (m *MockCtxError) Message() string { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Message") |
|
||||||
ret0, _ := ret[0].(string) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Message indicates an expected call of Message
|
|
||||||
func (mr *MockCtxErrorMockRecorder) Message() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Message", reflect.TypeOf((*MockCtxError)(nil).Message)) |
|
||||||
} |
|
||||||
|
|
||||||
// Contexts mocks base method
|
|
||||||
func (m *MockCtxError) Contexts() map[string]interface{} { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Contexts") |
|
||||||
ret0, _ := ret[0].(map[string]interface{}) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Contexts indicates an expected call of Contexts
|
|
||||||
func (mr *MockCtxErrorMockRecorder) Contexts() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Contexts", reflect.TypeOf((*MockCtxError)(nil).Contexts)) |
|
||||||
} |
|
||||||
|
|
||||||
// WithCause mocks base method
|
|
||||||
func (m *MockCtxError) WithCause(c error) ctxerror.CtxError { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "WithCause", c) |
|
||||||
ret0, _ := ret[0].(ctxerror.CtxError) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// WithCause indicates an expected call of WithCause
|
|
||||||
func (mr *MockCtxErrorMockRecorder) WithCause(c interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WithCause", reflect.TypeOf((*MockCtxError)(nil).WithCause), c) |
|
||||||
} |
|
||||||
|
|
||||||
// MockLog15Logger is a mock of Log15Logger interface
|
|
||||||
type MockLog15Logger struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockLog15LoggerMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockLog15LoggerMockRecorder is the mock recorder for MockLog15Logger
|
|
||||||
type MockLog15LoggerMockRecorder struct { |
|
||||||
mock *MockLog15Logger |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockLog15Logger creates a new mock instance
|
|
||||||
func NewMockLog15Logger(ctrl *gomock.Controller) *MockLog15Logger { |
|
||||||
mock := &MockLog15Logger{ctrl: ctrl} |
|
||||||
mock.recorder = &MockLog15LoggerMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockLog15Logger) EXPECT() *MockLog15LoggerMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Log15 mocks base method
|
|
||||||
func (m *MockLog15Logger) Log15(f ctxerror.Log15Func) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
m.ctrl.Call(m, "Log15", f) |
|
||||||
} |
|
||||||
|
|
||||||
// Log15 indicates an expected call of Log15
|
|
||||||
func (mr *MockLog15LoggerMockRecorder) Log15(f interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Log15", reflect.TypeOf((*MockLog15Logger)(nil).Log15), f) |
|
||||||
} |
|
@ -1,89 +0,0 @@ |
|||||||
package utils |
|
||||||
|
|
||||||
import ( |
|
||||||
"reflect" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log" |
|
||||||
"github.com/golang/mock/gomock" |
|
||||||
|
|
||||||
matchers "github.com/harmony-one/harmony/gomock_matchers" |
|
||||||
"github.com/harmony-one/harmony/internal/utils/mock_log" |
|
||||||
) |
|
||||||
|
|
||||||
//go:generate mockgen -destination mock_log/logger.go github.com/ethereum/go-ethereum/log Logger
|
|
||||||
//go:generate mockgen -destination mock_log/handler.go github.com/ethereum/go-ethereum/log Handler
|
|
||||||
|
|
||||||
const ( |
|
||||||
thisPkg = "github.com/harmony-one/harmony/internal/utils" |
|
||||||
thisFile = "logging_test.go" |
|
||||||
) |
|
||||||
|
|
||||||
func testWithCallerSkip0(t *testing.T, skip int, name string, line int) { |
|
||||||
ctrl := gomock.NewController(t) |
|
||||||
defer ctrl.Finish() |
|
||||||
want := log.Root() |
|
||||||
logger := mock_log.NewMockLogger(ctrl) |
|
||||||
logger.EXPECT().New(matchers.Slice{ |
|
||||||
"funcName", thisPkg + "." + name, |
|
||||||
"funcFile", thisFile, |
|
||||||
"funcLine", line, |
|
||||||
}).Return(want) |
|
||||||
if got := WithCallerSkip(logger, skip); !reflect.DeepEqual(got, want) { |
|
||||||
t.Errorf("WithCallerSkip() = %v, want %v", got, want) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func testWithCallerSkip1(t *testing.T, skip int, name string, line int) { |
|
||||||
testWithCallerSkip0(t, skip, name, line) |
|
||||||
} |
|
||||||
|
|
||||||
func testWithCallerSkip2(t *testing.T, skip int, name string, line int) { |
|
||||||
testWithCallerSkip1(t, skip, name, line) |
|
||||||
} |
|
||||||
|
|
||||||
func TestWithCallerSkip(t *testing.T) { |
|
||||||
t.Run("0", func(t *testing.T) { |
|
||||||
testWithCallerSkip2(t, 0, "testWithCallerSkip0", 32) |
|
||||||
}) |
|
||||||
t.Run("1", func(t *testing.T) { |
|
||||||
testWithCallerSkip2(t, 1, "testWithCallerSkip1", 38) |
|
||||||
}) |
|
||||||
t.Run("2", func(t *testing.T) { |
|
||||||
testWithCallerSkip2(t, 2, "testWithCallerSkip2", 42) |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestWithCaller(t *testing.T) { |
|
||||||
ctrl := gomock.NewController(t) |
|
||||||
defer ctrl.Finish() |
|
||||||
want := log.Root() |
|
||||||
logger := mock_log.NewMockLogger(ctrl) |
|
||||||
logger.EXPECT().New(matchers.Slice{ |
|
||||||
"funcName", thisPkg + ".TestWithCaller", |
|
||||||
"funcFile", thisFile, |
|
||||||
"funcLine", 67, // keep this in sync with WithCaller() call below
|
|
||||||
}).Return(want) |
|
||||||
if got := WithCaller(logger); !reflect.DeepEqual(got, want) { |
|
||||||
t.Errorf("WithCallerSkip() = %v, want %v", got, want) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestGetLogger(t *testing.T) { |
|
||||||
oldHandler := GetLogInstance().GetHandler() |
|
||||||
defer GetLogInstance().SetHandler(oldHandler) |
|
||||||
ctrl := gomock.NewController(t) |
|
||||||
handler := mock_log.NewMockHandler(ctrl) |
|
||||||
handler.EXPECT().Log(matchers.Struct{ |
|
||||||
"Msg": "omg", |
|
||||||
"Ctx": matchers.Slice{ |
|
||||||
"port", "", // added by the singleton instance
|
|
||||||
"ip", "", // added by the singleton instance
|
|
||||||
"funcName", thisPkg + ".TestGetLogger", |
|
||||||
"funcFile", thisFile, |
|
||||||
"funcLine", 88, // keep this in sync with Debug() call below
|
|
||||||
}, |
|
||||||
}) |
|
||||||
GetLogInstance().SetHandler(handler) |
|
||||||
GetLogger().Debug("omg") |
|
||||||
} |
|
@ -1,49 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: testing.go
|
|
||||||
|
|
||||||
// Package mock_utils is a generated GoMock package.
|
|
||||||
package mock_utils |
|
||||||
|
|
||||||
import ( |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockTestLogger is a mock of TestLogger interface
|
|
||||||
type MockTestLogger struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockTestLoggerMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockTestLoggerMockRecorder is the mock recorder for MockTestLogger
|
|
||||||
type MockTestLoggerMockRecorder struct { |
|
||||||
mock *MockTestLogger |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockTestLogger creates a new mock instance
|
|
||||||
func NewMockTestLogger(ctrl *gomock.Controller) *MockTestLogger { |
|
||||||
mock := &MockTestLogger{ctrl: ctrl} |
|
||||||
mock.recorder = &MockTestLoggerMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockTestLogger) EXPECT() *MockTestLoggerMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Log mocks base method
|
|
||||||
func (m *MockTestLogger) Log(args ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{} |
|
||||||
for _, a := range args { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Log", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Log indicates an expected call of Log
|
|
||||||
func (mr *MockTestLoggerMockRecorder) Log(args ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Log", reflect.TypeOf((*MockTestLogger)(nil).Log), args...) |
|
||||||
} |
|
@ -1,48 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: github.com/ethereum/go-ethereum/log (interfaces: Handler)
|
|
||||||
|
|
||||||
// Package mock_log is a generated GoMock package.
|
|
||||||
package mock_log |
|
||||||
|
|
||||||
import ( |
|
||||||
log "github.com/ethereum/go-ethereum/log" |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockHandler is a mock of Handler interface
|
|
||||||
type MockHandler struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockHandlerMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockHandlerMockRecorder is the mock recorder for MockHandler
|
|
||||||
type MockHandlerMockRecorder struct { |
|
||||||
mock *MockHandler |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockHandler creates a new mock instance
|
|
||||||
func NewMockHandler(ctrl *gomock.Controller) *MockHandler { |
|
||||||
mock := &MockHandler{ctrl: ctrl} |
|
||||||
mock.recorder = &MockHandlerMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockHandler) EXPECT() *MockHandlerMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Log mocks base method
|
|
||||||
func (m *MockHandler) Log(arg0 *log.Record) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Log", arg0) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Log indicates an expected call of Log
|
|
||||||
func (mr *MockHandlerMockRecorder) Log(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Log", reflect.TypeOf((*MockHandler)(nil).Log), arg0) |
|
||||||
} |
|
@ -1,180 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: github.com/ethereum/go-ethereum/log (interfaces: Logger)
|
|
||||||
|
|
||||||
// Package mock_log is a generated GoMock package.
|
|
||||||
package mock_log |
|
||||||
|
|
||||||
import ( |
|
||||||
log "github.com/ethereum/go-ethereum/log" |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockLogger is a mock of Logger interface
|
|
||||||
type MockLogger struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockLoggerMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockLoggerMockRecorder is the mock recorder for MockLogger
|
|
||||||
type MockLoggerMockRecorder struct { |
|
||||||
mock *MockLogger |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockLogger creates a new mock instance
|
|
||||||
func NewMockLogger(ctrl *gomock.Controller) *MockLogger { |
|
||||||
mock := &MockLogger{ctrl: ctrl} |
|
||||||
mock.recorder = &MockLoggerMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockLogger) EXPECT() *MockLoggerMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Crit mocks base method
|
|
||||||
func (m *MockLogger) Crit(arg0 string, arg1 ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{arg0} |
|
||||||
for _, a := range arg1 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Crit", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Crit indicates an expected call of Crit
|
|
||||||
func (mr *MockLoggerMockRecorder) Crit(arg0 interface{}, arg1 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
varargs := append([]interface{}{arg0}, arg1...) |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Crit", reflect.TypeOf((*MockLogger)(nil).Crit), varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Debug mocks base method
|
|
||||||
func (m *MockLogger) Debug(arg0 string, arg1 ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{arg0} |
|
||||||
for _, a := range arg1 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Debug", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Debug indicates an expected call of Debug
|
|
||||||
func (mr *MockLoggerMockRecorder) Debug(arg0 interface{}, arg1 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
varargs := append([]interface{}{arg0}, arg1...) |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Debug", reflect.TypeOf((*MockLogger)(nil).Debug), varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Error mocks base method
|
|
||||||
func (m *MockLogger) Error(arg0 string, arg1 ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{arg0} |
|
||||||
for _, a := range arg1 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Error", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Error indicates an expected call of Error
|
|
||||||
func (mr *MockLoggerMockRecorder) Error(arg0 interface{}, arg1 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
varargs := append([]interface{}{arg0}, arg1...) |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Error", reflect.TypeOf((*MockLogger)(nil).Error), varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// GetHandler mocks base method
|
|
||||||
func (m *MockLogger) GetHandler() log.Handler { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetHandler") |
|
||||||
ret0, _ := ret[0].(log.Handler) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetHandler indicates an expected call of GetHandler
|
|
||||||
func (mr *MockLoggerMockRecorder) GetHandler() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetHandler", reflect.TypeOf((*MockLogger)(nil).GetHandler)) |
|
||||||
} |
|
||||||
|
|
||||||
// Info mocks base method
|
|
||||||
func (m *MockLogger) Info(arg0 string, arg1 ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{arg0} |
|
||||||
for _, a := range arg1 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Info", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Info indicates an expected call of Info
|
|
||||||
func (mr *MockLoggerMockRecorder) Info(arg0 interface{}, arg1 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
varargs := append([]interface{}{arg0}, arg1...) |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockLogger)(nil).Info), varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// New mocks base method
|
|
||||||
func (m *MockLogger) New(arg0 ...interface{}) log.Logger { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{} |
|
||||||
for _, a := range arg0 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
ret := m.ctrl.Call(m, "New", varargs...) |
|
||||||
ret0, _ := ret[0].(log.Logger) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// New indicates an expected call of New
|
|
||||||
func (mr *MockLoggerMockRecorder) New(arg0 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "New", reflect.TypeOf((*MockLogger)(nil).New), arg0...) |
|
||||||
} |
|
||||||
|
|
||||||
// SetHandler mocks base method
|
|
||||||
func (m *MockLogger) SetHandler(arg0 log.Handler) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
m.ctrl.Call(m, "SetHandler", arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// SetHandler indicates an expected call of SetHandler
|
|
||||||
func (mr *MockLoggerMockRecorder) SetHandler(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetHandler", reflect.TypeOf((*MockLogger)(nil).SetHandler), arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// Trace mocks base method
|
|
||||||
func (m *MockLogger) Trace(arg0 string, arg1 ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{arg0} |
|
||||||
for _, a := range arg1 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Trace", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Trace indicates an expected call of Trace
|
|
||||||
func (mr *MockLoggerMockRecorder) Trace(arg0 interface{}, arg1 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
varargs := append([]interface{}{arg0}, arg1...) |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Trace", reflect.TypeOf((*MockLogger)(nil).Trace), varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Warn mocks base method
|
|
||||||
func (m *MockLogger) Warn(arg0 string, arg1 ...interface{}) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
varargs := []interface{}{arg0} |
|
||||||
for _, a := range arg1 { |
|
||||||
varargs = append(varargs, a) |
|
||||||
} |
|
||||||
m.ctrl.Call(m, "Warn", varargs...) |
|
||||||
} |
|
||||||
|
|
||||||
// Warn indicates an expected call of Warn
|
|
||||||
func (mr *MockLoggerMockRecorder) Warn(arg0 interface{}, arg1 ...interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
varargs := append([]interface{}{arg0}, arg1...) |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLogger)(nil).Warn), varargs...) |
|
||||||
} |
|
@ -1,69 +0,0 @@ |
|||||||
package utils |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"strings" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log" |
|
||||||
) |
|
||||||
|
|
||||||
//go:generate mockgen -source=testing.go -destination=mock/testing.go
|
|
||||||
|
|
||||||
// TestLogRedirector is a log15-compatible log handler that logs to the testing
|
|
||||||
// object. It is useful when debugging, because it surfaces application log
|
|
||||||
// messages – such as ones emitted by the SUT – which are otherwise discarded
|
|
||||||
// and not displayed during testing.
|
|
||||||
//
|
|
||||||
// Typical usage:
|
|
||||||
//
|
|
||||||
// func TestMyFunc(t *testing.T) {
|
|
||||||
// l := utils.GetLogInstance()
|
|
||||||
// lrd := NewTestLogRedirector(l, t)
|
|
||||||
// defer lrd.Close()
|
|
||||||
//
|
|
||||||
// // Everything sent to the logger l will be printed onto the test log,
|
|
||||||
// // until lrd.Close() is called at the end of the function.
|
|
||||||
// // Contexts are formatted using the "%#v" format specifier.
|
|
||||||
// l.Debug("hello", "audience", "world")
|
|
||||||
// // The above prints: hello, audience="world"
|
|
||||||
// }
|
|
||||||
type TestLogRedirector struct { |
|
||||||
l log.Logger |
|
||||||
h log.Handler |
|
||||||
t TestLogger |
|
||||||
} |
|
||||||
|
|
||||||
// TestLogger is the logging interface implemented by testing.T.
|
|
||||||
type TestLogger interface { |
|
||||||
Log(args ...interface{}) |
|
||||||
} |
|
||||||
|
|
||||||
// NewTestLogRedirector returns a new testing log redirector.
|
|
||||||
// The given logger's handler is saved and replaced by the receiver.
|
|
||||||
// Caller shall ensure Close() is called when the redirector is no longer
|
|
||||||
// needed.
|
|
||||||
func NewTestLogRedirector(l log.Logger, t TestLogger) *TestLogRedirector { |
|
||||||
r := &TestLogRedirector{l: l, h: l.GetHandler(), t: t} |
|
||||||
l.SetHandler(r) |
|
||||||
return r |
|
||||||
} |
|
||||||
|
|
||||||
// Log logs the given log15 record into the testing object.
|
|
||||||
func (redirector *TestLogRedirector) Log(r *log.Record) error { |
|
||||||
segments := []string{fmt.Sprintf("[%s] %s", r.Lvl.String(), r.Msg)} |
|
||||||
for i := 0; i+1 < len(r.Ctx); i += 2 { |
|
||||||
segments = append(segments, fmt.Sprintf("%s=%#v", r.Ctx[i], r.Ctx[i+1])) |
|
||||||
} |
|
||||||
redirector.t.Log(strings.Join(segments, ", ")) |
|
||||||
return nil |
|
||||||
} |
|
||||||
|
|
||||||
// Close restores the log handler back to the original one.
|
|
||||||
func (redirector *TestLogRedirector) Close() error { |
|
||||||
if redirector.l != nil { |
|
||||||
redirector.l.SetHandler(redirector.h) |
|
||||||
redirector.l = nil |
|
||||||
redirector.h = nil |
|
||||||
} |
|
||||||
return nil |
|
||||||
} |
|
@ -1,106 +0,0 @@ |
|||||||
package utils |
|
||||||
|
|
||||||
import ( |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log" |
|
||||||
"github.com/golang/mock/gomock" |
|
||||||
|
|
||||||
mock_utils "github.com/harmony-one/harmony/internal/utils/mock" |
|
||||||
) |
|
||||||
|
|
||||||
func TestExampleTestLogRedirectorUsingGetLogInstance(t *testing.T) { |
|
||||||
tlr := NewTestLogRedirector(GetLogInstance(), t) |
|
||||||
defer tlr.Close() |
|
||||||
|
|
||||||
// Everything logged via GetLogInstance(),
|
|
||||||
// including lines said by the Menethil family below,
|
|
||||||
// is forwarded to the testing object.
|
|
||||||
// You will see their lines when you run this “test.”
|
|
||||||
|
|
||||||
terenasMenethil() |
|
||||||
arthasMenethil() |
|
||||||
} |
|
||||||
|
|
||||||
func terenasMenethil() { |
|
||||||
GetLogInstance().Info("what are you doing my son") |
|
||||||
} |
|
||||||
|
|
||||||
func arthasMenethil() { |
|
||||||
GetLogInstance().Warn("succeding you father") |
|
||||||
} |
|
||||||
|
|
||||||
func TestNewTestLogRedirector(t *testing.T) { |
|
||||||
logger := log.New() |
|
||||||
origHandler := logger.GetHandler() |
|
||||||
tlr := NewTestLogRedirector(logger, t) |
|
||||||
if tlr == nil { |
|
||||||
t.Fatalf("NewTestLogRedirector() = nil") |
|
||||||
} |
|
||||||
if tlr.l != logger { |
|
||||||
t.Errorf("NewTestLogRedirector().l = %v, want %v", tlr.l, logger) |
|
||||||
} |
|
||||||
if tlr.h != origHandler { |
|
||||||
t.Errorf("NewTestLogRedirector().h = %v, want %v", tlr.h, origHandler) |
|
||||||
} |
|
||||||
if tlr.t != t { |
|
||||||
t.Errorf("NewTestLogRedirector().t = %v, want %v", tlr.t, t) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestTestLogRedirector_Log(t *testing.T) { |
|
||||||
type args struct { |
|
||||||
r *log.Record |
|
||||||
} |
|
||||||
tests := []struct { |
|
||||||
name string |
|
||||||
rec log.Record |
|
||||||
want string |
|
||||||
}{ |
|
||||||
{ |
|
||||||
"WithoutContexts", |
|
||||||
log.Record{Lvl: log.LvlDebug, Msg: "hello world"}, |
|
||||||
`[dbug] hello world`, |
|
||||||
}, |
|
||||||
{ |
|
||||||
"WithContexts", |
|
||||||
log.Record{Lvl: log.LvlWarn, Msg: "hello", Ctx: []interface{}{ |
|
||||||
"audience", "world", |
|
||||||
"pid", 31337, |
|
||||||
}}, |
|
||||||
`[warn] hello, audience="world", pid=31337`, |
|
||||||
}, |
|
||||||
} |
|
||||||
for _, tt := range tests { |
|
||||||
t.Run(tt.name, func(t *testing.T) { |
|
||||||
ctrl := gomock.NewController(t) |
|
||||||
defer ctrl.Finish() |
|
||||||
mockTestLogger := mock_utils.NewMockTestLogger(ctrl) |
|
||||||
redirector := NewTestLogRedirector(log.New(), mockTestLogger) |
|
||||||
mockTestLogger.EXPECT().Log(tt.want) |
|
||||||
if err := redirector.Log(&tt.rec); err != nil { |
|
||||||
t.Errorf("TestLogRedirector.Log() error = %v, want nil", err) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestTestLogRedirector_Close(t *testing.T) { |
|
||||||
ctrl := gomock.NewController(t) |
|
||||||
defer ctrl.Finish() |
|
||||||
mockTestLogger := mock_utils.NewMockTestLogger(ctrl) |
|
||||||
logger := log.New() |
|
||||||
tlr := NewTestLogRedirector(logger, mockTestLogger) |
|
||||||
mockTestLogger.EXPECT().Log("[info] before close").Times(1) |
|
||||||
mockTestLogger.EXPECT().Log("[info] after close").Times(0) |
|
||||||
mockTestLogger.EXPECT().Log("[info] after idempotent close").Times(0) |
|
||||||
logger.Info("before close") |
|
||||||
if err := tlr.Close(); err != nil { |
|
||||||
t.Errorf("TestLogRedirector.Close() error = %v, want nil", err) |
|
||||||
} |
|
||||||
logger.Info("after close") |
|
||||||
if err := tlr.Close(); err != nil { |
|
||||||
t.Errorf("TestLogRedirector.Close() error = %v, want nil", err) |
|
||||||
} |
|
||||||
logger.Info("after idempotent close") |
|
||||||
} |
|
@ -1,152 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: hostv2.go
|
|
||||||
|
|
||||||
// Package hostv2 is a generated GoMock package.
|
|
||||||
package hostv2 |
|
||||||
|
|
||||||
import ( |
|
||||||
context "context" |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
go_libp2p_pubsub "github.com/libp2p/go-libp2p-pubsub" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MocktopicHandle is a mock of topicHandle interface
|
|
||||||
type MocktopicHandle struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MocktopicHandleMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MocktopicHandleMockRecorder is the mock recorder for MocktopicHandle
|
|
||||||
type MocktopicHandleMockRecorder struct { |
|
||||||
mock *MocktopicHandle |
|
||||||
} |
|
||||||
|
|
||||||
// NewMocktopicHandle creates a new mock instance
|
|
||||||
func NewMocktopicHandle(ctrl *gomock.Controller) *MocktopicHandle { |
|
||||||
mock := &MocktopicHandle{ctrl: ctrl} |
|
||||||
mock.recorder = &MocktopicHandleMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MocktopicHandle) EXPECT() *MocktopicHandleMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Publish mocks base method
|
|
||||||
func (m *MocktopicHandle) Publish(ctx context.Context, data []byte) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Publish", ctx, data) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Publish indicates an expected call of Publish
|
|
||||||
func (mr *MocktopicHandleMockRecorder) Publish(ctx, data interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Publish", reflect.TypeOf((*MocktopicHandle)(nil).Publish), ctx, data) |
|
||||||
} |
|
||||||
|
|
||||||
// Subscribe mocks base method
|
|
||||||
func (m *MocktopicHandle) Subscribe() (subscription, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Subscribe") |
|
||||||
ret0, _ := ret[0].(subscription) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// Subscribe indicates an expected call of Subscribe
|
|
||||||
func (mr *MocktopicHandleMockRecorder) Subscribe() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Subscribe", reflect.TypeOf((*MocktopicHandle)(nil).Subscribe)) |
|
||||||
} |
|
||||||
|
|
||||||
// MocktopicJoiner is a mock of topicJoiner interface
|
|
||||||
type MocktopicJoiner struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MocktopicJoinerMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MocktopicJoinerMockRecorder is the mock recorder for MocktopicJoiner
|
|
||||||
type MocktopicJoinerMockRecorder struct { |
|
||||||
mock *MocktopicJoiner |
|
||||||
} |
|
||||||
|
|
||||||
// NewMocktopicJoiner creates a new mock instance
|
|
||||||
func NewMocktopicJoiner(ctrl *gomock.Controller) *MocktopicJoiner { |
|
||||||
mock := &MocktopicJoiner{ctrl: ctrl} |
|
||||||
mock.recorder = &MocktopicJoinerMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MocktopicJoiner) EXPECT() *MocktopicJoinerMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// JoinTopic mocks base method
|
|
||||||
func (m *MocktopicJoiner) JoinTopic(topic string) (topicHandle, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "JoinTopic", topic) |
|
||||||
ret0, _ := ret[0].(topicHandle) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// JoinTopic indicates an expected call of JoinTopic
|
|
||||||
func (mr *MocktopicJoinerMockRecorder) JoinTopic(topic interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "JoinTopic", reflect.TypeOf((*MocktopicJoiner)(nil).JoinTopic), topic) |
|
||||||
} |
|
||||||
|
|
||||||
// Mocksubscription is a mock of subscription interface
|
|
||||||
type Mocksubscription struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MocksubscriptionMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MocksubscriptionMockRecorder is the mock recorder for Mocksubscription
|
|
||||||
type MocksubscriptionMockRecorder struct { |
|
||||||
mock *Mocksubscription |
|
||||||
} |
|
||||||
|
|
||||||
// NewMocksubscription creates a new mock instance
|
|
||||||
func NewMocksubscription(ctrl *gomock.Controller) *Mocksubscription { |
|
||||||
mock := &Mocksubscription{ctrl: ctrl} |
|
||||||
mock.recorder = &MocksubscriptionMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *Mocksubscription) EXPECT() *MocksubscriptionMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Next mocks base method
|
|
||||||
func (m *Mocksubscription) Next(ctx context.Context) (*go_libp2p_pubsub.Message, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Next", ctx) |
|
||||||
ret0, _ := ret[0].(*go_libp2p_pubsub.Message) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// Next indicates an expected call of Next
|
|
||||||
func (mr *MocksubscriptionMockRecorder) Next(ctx interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Next", reflect.TypeOf((*Mocksubscription)(nil).Next), ctx) |
|
||||||
} |
|
||||||
|
|
||||||
// Cancel mocks base method
|
|
||||||
func (m *Mocksubscription) Cancel() { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
m.ctrl.Call(m, "Cancel") |
|
||||||
} |
|
||||||
|
|
||||||
// Cancel indicates an expected call of Cancel
|
|
||||||
func (mr *MocksubscriptionMockRecorder) Cancel() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Cancel", reflect.TypeOf((*Mocksubscription)(nil).Cancel)) |
|
||||||
} |
|
@ -1,299 +0,0 @@ |
|||||||
package hostv2 |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"errors" |
|
||||||
"reflect" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/golang/mock/gomock" |
|
||||||
libp2p_peer "github.com/libp2p/go-libp2p-core/peer" |
|
||||||
libp2p_pubsub "github.com/libp2p/go-libp2p-pubsub" |
|
||||||
libp2p_pubsub_pb "github.com/libp2p/go-libp2p-pubsub/pb" |
|
||||||
|
|
||||||
nodeconfig "github.com/harmony-one/harmony/internal/configs/node" |
|
||||||
) |
|
||||||
|
|
||||||
func TestHostV2_SendMessageToGroups(t *testing.T) { |
|
||||||
t.Run("Basic", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
okTopic := NewMocktopicHandle(mc) |
|
||||||
newTopic := NewMocktopicHandle(mc) |
|
||||||
groups := []nodeconfig.GroupID{"OK", "New"} |
|
||||||
data := []byte{1, 2, 3} |
|
||||||
joined := map[string]topicHandle{"OK": okTopic} |
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: joined} |
|
||||||
|
|
||||||
gomock.InOrder( |
|
||||||
// okTopic is already in joined map, JoinTopic shouldn't be called
|
|
||||||
joiner.EXPECT().JoinTopic("OK").Times(0), |
|
||||||
okTopic.EXPECT().Publish(context.TODO(), data).Return(nil), |
|
||||||
// newTopic is not in joined map, JoinTopic should be called
|
|
||||||
joiner.EXPECT().JoinTopic("New").Return(newTopic, nil), |
|
||||||
newTopic.EXPECT().Publish(context.TODO(), data).Return(nil), |
|
||||||
) |
|
||||||
|
|
||||||
err := host.SendMessageToGroups(groups, data) |
|
||||||
|
|
||||||
if err != nil { |
|
||||||
t.Errorf("expected no error; got %v", err) |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("JoinError", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
okTopic := NewMocktopicHandle(mc) |
|
||||||
groups := []nodeconfig.GroupID{"Error", "OK"} |
|
||||||
data := []byte{1, 2, 3} |
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
gomock.InOrder( |
|
||||||
// Make first join return an error
|
|
||||||
joiner.EXPECT().JoinTopic("Error").Return(nil, errors.New("join error")), |
|
||||||
// Subsequent topics should still be processed after an error
|
|
||||||
joiner.EXPECT().JoinTopic("OK").Return(okTopic, nil), |
|
||||||
okTopic.EXPECT().Publish(context.TODO(), data).Return(nil), |
|
||||||
) |
|
||||||
|
|
||||||
err := host.SendMessageToGroups(groups, data) |
|
||||||
|
|
||||||
if err == nil { |
|
||||||
t.Error("expected an error; got nil") |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("PublishError", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
okTopic := NewMocktopicHandle(mc) |
|
||||||
erringTopic := NewMocktopicHandle(mc) |
|
||||||
groups := []nodeconfig.GroupID{"Error", "OK"} |
|
||||||
data := []byte{1, 2, 3} |
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
gomock.InOrder( |
|
||||||
// Make first publish return an error
|
|
||||||
joiner.EXPECT().JoinTopic("Error").Return(erringTopic, nil), |
|
||||||
erringTopic.EXPECT().Publish(context.TODO(), data).Return(errors.New("publish error")), |
|
||||||
// Subsequent topics should still be processed after an error
|
|
||||||
joiner.EXPECT().JoinTopic("OK").Return(okTopic, nil), |
|
||||||
okTopic.EXPECT().Publish(context.TODO(), data).Return(nil), |
|
||||||
) |
|
||||||
|
|
||||||
if err := host.SendMessageToGroups(groups, data); err == nil { |
|
||||||
t.Error("expected an error; got nil") |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestGroupReceiver_Close(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
sub := NewMocksubscription(mc) |
|
||||||
sub.EXPECT().Cancel() |
|
||||||
receiver := GroupReceiverImpl{sub: sub} |
|
||||||
|
|
||||||
err := receiver.Close() |
|
||||||
|
|
||||||
if err != nil { |
|
||||||
t.Errorf("expected no error but got %v", err) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func pubsubMessage(from libp2p_peer.ID, data []byte) *libp2p_pubsub.Message { |
|
||||||
m := libp2p_pubsub_pb.Message{From: []byte(from), Data: data} |
|
||||||
return &libp2p_pubsub.Message{Message: &m} |
|
||||||
} |
|
||||||
|
|
||||||
func TestGroupReceiver_Receive(t *testing.T) { |
|
||||||
t.Run("OK", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
ctx := context.Background() |
|
||||||
sub := NewMocksubscription(mc) |
|
||||||
receiver := GroupReceiverImpl{sub: sub} |
|
||||||
wantSender := libp2p_peer.ID("OK") |
|
||||||
wantMsg := []byte{1, 2, 3} |
|
||||||
|
|
||||||
sub.EXPECT().Next(ctx).Return(pubsubMessage(wantSender, wantMsg), nil) |
|
||||||
|
|
||||||
gotMsg, gotSender, err := receiver.Receive(ctx) |
|
||||||
|
|
||||||
if err != nil { |
|
||||||
t.Errorf("expected no error; got %v", err) |
|
||||||
} |
|
||||||
if gotSender != wantSender { |
|
||||||
t.Errorf("expected sender %v; got %v", wantSender, gotSender) |
|
||||||
} |
|
||||||
if !reflect.DeepEqual(gotMsg, wantMsg) { |
|
||||||
t.Errorf("expected message %v; got %v", wantMsg, gotMsg) |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("Error", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
ctx := context.Background() |
|
||||||
sub := NewMocksubscription(mc) |
|
||||||
receiver := GroupReceiverImpl{sub: sub} |
|
||||||
|
|
||||||
sub.EXPECT().Next(ctx).Return(nil, errors.New("receive error")) |
|
||||||
|
|
||||||
msg, sender, err := receiver.Receive(ctx) |
|
||||||
|
|
||||||
if err == nil { |
|
||||||
t.Error("expected an error; got nil") |
|
||||||
} |
|
||||||
if sender != "" { |
|
||||||
t.Errorf("expected empty sender; got %v", sender) |
|
||||||
} |
|
||||||
if len(msg) > 0 { |
|
||||||
t.Errorf("expected empty message; got %v", msg) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestHostV2_GroupReceiver(t *testing.T) { |
|
||||||
t.Run("New", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
sub := &libp2p_pubsub.Subscription{} |
|
||||||
topic := NewMocktopicHandle(mc) |
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
gomock.InOrder( |
|
||||||
joiner.EXPECT().JoinTopic("ABC").Return(topic, nil), |
|
||||||
topic.EXPECT().Subscribe().Return(sub, nil), |
|
||||||
) |
|
||||||
|
|
||||||
gotReceiver, err := host.GroupReceiver("ABC") |
|
||||||
|
|
||||||
if r, ok := gotReceiver.(*GroupReceiverImpl); !ok { |
|
||||||
t.Errorf("expected a hostv2 GroupReceiverImpl; got %v", gotReceiver) |
|
||||||
} else if r.sub != sub { |
|
||||||
t.Errorf("unexpected subscriber %v", r.sub) |
|
||||||
} |
|
||||||
if err != nil { |
|
||||||
t.Errorf("expected no error; got %v", err) |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("JoinError", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
joiner.EXPECT().JoinTopic("ABC").Return(nil, errors.New("join error")) |
|
||||||
|
|
||||||
gotReceiver, err := host.GroupReceiver("ABC") |
|
||||||
|
|
||||||
if gotReceiver != nil { |
|
||||||
t.Errorf("expected a nil hostv2 GroupReceiverImpl; got %v", gotReceiver) |
|
||||||
} |
|
||||||
if err == nil { |
|
||||||
t.Error("expected an error; got none") |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("SubscribeError", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
topic := NewMocktopicHandle(mc) |
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
gomock.InOrder( |
|
||||||
joiner.EXPECT().JoinTopic("ABC").Return(topic, nil), |
|
||||||
topic.EXPECT().Subscribe().Return(nil, errors.New("subscription error")), |
|
||||||
) |
|
||||||
|
|
||||||
gotReceiver, err := host.GroupReceiver("ABC") |
|
||||||
|
|
||||||
if gotReceiver != nil { |
|
||||||
t.Errorf("expected a nil hostv2 GroupReceiverImpl; got %v", gotReceiver) |
|
||||||
} |
|
||||||
if err == nil { |
|
||||||
t.Error("expected an error; got none") |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("Closed", func(t *testing.T) { |
|
||||||
var emptyReceiver GroupReceiverImpl |
|
||||||
_, _, err := emptyReceiver.Receive(context.Background()) |
|
||||||
if err == nil { |
|
||||||
t.Errorf("Receive() from nil/closed receiver did not return error") |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
||||||
|
|
||||||
func TestHostV2_getTopic(t *testing.T) { |
|
||||||
t.Run("NewOK", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
want := NewMocktopicHandle(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
joiner.EXPECT().JoinTopic("ABC").Return(want, nil) |
|
||||||
|
|
||||||
got, err := host.getTopic("ABC") |
|
||||||
|
|
||||||
if err != nil { |
|
||||||
t.Errorf("want nil error; got %v", err) |
|
||||||
} |
|
||||||
if got != want { |
|
||||||
t.Errorf("want topic handle %v; got %v", want, got) |
|
||||||
} |
|
||||||
if _, ok := host.joined["ABC"]; !ok { |
|
||||||
t.Error("topic not found in joined map") |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("NewError", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{}} |
|
||||||
|
|
||||||
joiner.EXPECT().JoinTopic("ABC").Return(nil, errors.New("OMG")) |
|
||||||
|
|
||||||
got, err := host.getTopic("ABC") |
|
||||||
|
|
||||||
if err == nil { |
|
||||||
t.Error("want non-nil error; got nil") |
|
||||||
} |
|
||||||
if got != nil { |
|
||||||
t.Errorf("want nil handle; got %v", got) |
|
||||||
} |
|
||||||
}) |
|
||||||
t.Run("Existing", func(t *testing.T) { |
|
||||||
mc := gomock.NewController(t) |
|
||||||
defer mc.Finish() |
|
||||||
|
|
||||||
joiner := NewMocktopicJoiner(mc) |
|
||||||
want := NewMocktopicHandle(mc) |
|
||||||
host := &HostV2{joiner: joiner, joined: map[string]topicHandle{"ABC": want}} |
|
||||||
|
|
||||||
joiner.EXPECT().JoinTopic("ABC").Times(0) |
|
||||||
|
|
||||||
got, err := host.getTopic("ABC") |
|
||||||
|
|
||||||
if err != nil { |
|
||||||
t.Errorf("want nil error; got %v", err) |
|
||||||
} |
|
||||||
if got != want { |
|
||||||
t.Errorf("want topic handle %v; got %v", want, got) |
|
||||||
} |
|
||||||
}) |
|
||||||
} |
|
@ -1,162 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: host.go
|
|
||||||
|
|
||||||
// Package mock_p2p is a generated GoMock package.
|
|
||||||
package mock_p2p |
|
||||||
|
|
||||||
import ( |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
node "github.com/harmony-one/harmony/internal/configs/node" |
|
||||||
p2p "github.com/harmony-one/harmony/p2p" |
|
||||||
go_libp2p_host "github.com/libp2p/go-libp2p-host" |
|
||||||
go_libp2p_peer "github.com/libp2p/go-libp2p-peer" |
|
||||||
reflect "reflect" |
|
||||||
) |
|
||||||
|
|
||||||
// MockHost is a mock of Host interface
|
|
||||||
type MockHost struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockHostMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockHostMockRecorder is the mock recorder for MockHost
|
|
||||||
type MockHostMockRecorder struct { |
|
||||||
mock *MockHost |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockHost creates a new mock instance
|
|
||||||
func NewMockHost(ctrl *gomock.Controller) *MockHost { |
|
||||||
mock := &MockHost{ctrl: ctrl} |
|
||||||
mock.recorder = &MockHostMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockHost) EXPECT() *MockHostMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// GetSelfPeer mocks base method
|
|
||||||
func (m *MockHost) GetSelfPeer() p2p.Peer { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetSelfPeer") |
|
||||||
ret0, _ := ret[0].(p2p.Peer) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetSelfPeer indicates an expected call of GetSelfPeer
|
|
||||||
func (mr *MockHostMockRecorder) GetSelfPeer() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetSelfPeer", reflect.TypeOf((*MockHost)(nil).GetSelfPeer)) |
|
||||||
} |
|
||||||
|
|
||||||
// Close mocks base method
|
|
||||||
func (m *MockHost) Close() error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Close") |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Close indicates an expected call of Close
|
|
||||||
func (mr *MockHostMockRecorder) Close() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockHost)(nil).Close)) |
|
||||||
} |
|
||||||
|
|
||||||
// AddPeer mocks base method
|
|
||||||
func (m *MockHost) AddPeer(arg0 *p2p.Peer) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "AddPeer", arg0) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// AddPeer indicates an expected call of AddPeer
|
|
||||||
func (mr *MockHostMockRecorder) AddPeer(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddPeer", reflect.TypeOf((*MockHost)(nil).AddPeer), arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// GetID mocks base method
|
|
||||||
func (m *MockHost) GetID() go_libp2p_peer.ID { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetID") |
|
||||||
ret0, _ := ret[0].(go_libp2p_peer.ID) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetID indicates an expected call of GetID
|
|
||||||
func (mr *MockHostMockRecorder) GetID() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetID", reflect.TypeOf((*MockHost)(nil).GetID)) |
|
||||||
} |
|
||||||
|
|
||||||
// GetP2PHost mocks base method
|
|
||||||
func (m *MockHost) GetP2PHost() go_libp2p_host.Host { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetP2PHost") |
|
||||||
ret0, _ := ret[0].(go_libp2p_host.Host) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetP2PHost indicates an expected call of GetP2PHost
|
|
||||||
func (mr *MockHostMockRecorder) GetP2PHost() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetP2PHost", reflect.TypeOf((*MockHost)(nil).GetP2PHost)) |
|
||||||
} |
|
||||||
|
|
||||||
// GetPeerCount mocks base method
|
|
||||||
func (m *MockHost) GetPeerCount() int { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GetPeerCount") |
|
||||||
ret0, _ := ret[0].(int) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// GetPeerCount indicates an expected call of GetPeerCount
|
|
||||||
func (mr *MockHostMockRecorder) GetPeerCount() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPeerCount", reflect.TypeOf((*MockHost)(nil).GetPeerCount)) |
|
||||||
} |
|
||||||
|
|
||||||
// ConnectHostPeer mocks base method
|
|
||||||
func (m *MockHost) ConnectHostPeer(arg0 p2p.Peer) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
m.ctrl.Call(m, "ConnectHostPeer", arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// ConnectHostPeer indicates an expected call of ConnectHostPeer
|
|
||||||
func (mr *MockHostMockRecorder) ConnectHostPeer(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConnectHostPeer", reflect.TypeOf((*MockHost)(nil).ConnectHostPeer), arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// SendMessageToGroups mocks base method
|
|
||||||
func (m *MockHost) SendMessageToGroups(groups []node.GroupID, msg []byte) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "SendMessageToGroups", groups, msg) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// SendMessageToGroups indicates an expected call of SendMessageToGroups
|
|
||||||
func (mr *MockHostMockRecorder) SendMessageToGroups(groups, msg interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SendMessageToGroups", reflect.TypeOf((*MockHost)(nil).SendMessageToGroups), groups, msg) |
|
||||||
} |
|
||||||
|
|
||||||
// GroupReceiver mocks base method
|
|
||||||
func (m *MockHost) GroupReceiver(arg0 node.GroupID) (p2p.GroupReceiver, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "GroupReceiver", arg0) |
|
||||||
ret0, _ := ret[0].(p2p.GroupReceiver) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// GroupReceiver indicates an expected call of GroupReceiver
|
|
||||||
func (mr *MockHostMockRecorder) GroupReceiver(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GroupReceiver", reflect.TypeOf((*MockHost)(nil).GroupReceiver), arg0) |
|
||||||
} |
|
@ -1,92 +0,0 @@ |
|||||||
// Code generated by MockGen. DO NOT EDIT.
|
|
||||||
// Source: stream.go
|
|
||||||
|
|
||||||
// Package p2p is a generated GoMock package.
|
|
||||||
package p2p |
|
||||||
|
|
||||||
import ( |
|
||||||
gomock "github.com/golang/mock/gomock" |
|
||||||
reflect "reflect" |
|
||||||
time "time" |
|
||||||
) |
|
||||||
|
|
||||||
// MockStream is a mock of Stream interface
|
|
||||||
type MockStream struct { |
|
||||||
ctrl *gomock.Controller |
|
||||||
recorder *MockStreamMockRecorder |
|
||||||
} |
|
||||||
|
|
||||||
// MockStreamMockRecorder is the mock recorder for MockStream
|
|
||||||
type MockStreamMockRecorder struct { |
|
||||||
mock *MockStream |
|
||||||
} |
|
||||||
|
|
||||||
// NewMockStream creates a new mock instance
|
|
||||||
func NewMockStream(ctrl *gomock.Controller) *MockStream { |
|
||||||
mock := &MockStream{ctrl: ctrl} |
|
||||||
mock.recorder = &MockStreamMockRecorder{mock} |
|
||||||
return mock |
|
||||||
} |
|
||||||
|
|
||||||
// EXPECT returns an object that allows the caller to indicate expected use
|
|
||||||
func (m *MockStream) EXPECT() *MockStreamMockRecorder { |
|
||||||
return m.recorder |
|
||||||
} |
|
||||||
|
|
||||||
// Read mocks base method
|
|
||||||
func (m *MockStream) Read(arg0 []byte) (int, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Read", arg0) |
|
||||||
ret0, _ := ret[0].(int) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// Read indicates an expected call of Read
|
|
||||||
func (mr *MockStreamMockRecorder) Read(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockStream)(nil).Read), arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// Write mocks base method
|
|
||||||
func (m *MockStream) Write(arg0 []byte) (int, error) { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Write", arg0) |
|
||||||
ret0, _ := ret[0].(int) |
|
||||||
ret1, _ := ret[1].(error) |
|
||||||
return ret0, ret1 |
|
||||||
} |
|
||||||
|
|
||||||
// Write indicates an expected call of Write
|
|
||||||
func (mr *MockStreamMockRecorder) Write(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockStream)(nil).Write), arg0) |
|
||||||
} |
|
||||||
|
|
||||||
// Close mocks base method
|
|
||||||
func (m *MockStream) Close() error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "Close") |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// Close indicates an expected call of Close
|
|
||||||
func (mr *MockStreamMockRecorder) Close() *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockStream)(nil).Close)) |
|
||||||
} |
|
||||||
|
|
||||||
// SetReadDeadline mocks base method
|
|
||||||
func (m *MockStream) SetReadDeadline(arg0 time.Time) error { |
|
||||||
m.ctrl.T.Helper() |
|
||||||
ret := m.ctrl.Call(m, "SetReadDeadline", arg0) |
|
||||||
ret0, _ := ret[0].(error) |
|
||||||
return ret0 |
|
||||||
} |
|
||||||
|
|
||||||
// SetReadDeadline indicates an expected call of SetReadDeadline
|
|
||||||
func (mr *MockStreamMockRecorder) SetReadDeadline(arg0 interface{}) *gomock.Call { |
|
||||||
mr.mock.ctrl.T.Helper() |
|
||||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetReadDeadline", reflect.TypeOf((*MockStream)(nil).SetReadDeadline), arg0) |
|
||||||
} |
|
Loading…
Reference in new issue