| 1 |
package detector |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
type testDetector string |
| 10 |
|
| 11 |
func (d testDetector) Start() error { return nil } |
| 12 |
func (d testDetector) Detect(f MasterChanged) error { return nil } |
| 13 |
|
| 14 |
func (d testDetector) Done() <-chan struct{} { return make(<-chan struct{}) } |
| 15 |
func (d testDetector) Cancel() {} |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func Unregister(prefix string) { |
| 21 |
pluginLock.Lock() |
| 22 |
defer pluginLock.Unlock() |
| 23 |
delete(plugins, prefix) |
| 24 |
} |
| 25 |
|
| 26 |
func TestDetectorFactoryRegister(t *testing.T) { |
| 27 |
prefix := "bbm:" |
| 28 |
Register(prefix, func(spec string, _ ...Option) (Master, error) { |
| 29 |
return testDetector("Hello!"), nil |
| 30 |
}) |
| 31 |
defer Unregister(prefix) |
| 32 |
|
| 33 |
f, ok := MatchingPlugin(prefix) |
| 34 |
|
| 35 |
assert.True(t, ok) |
| 36 |
assert.NotNil(t, f) |
| 37 |
} |
| 38 |
|
| 39 |
func TestDectorFactoryNew_EmptySpec(t *testing.T) { |
| 40 |
assert := assert.New(t) |
| 41 |
m, err := New("") |
| 42 |
assert.NotNil(err) |
| 43 |
assert.Nil(m) |
| 44 |
} |
| 45 |
|
| 46 |
func TestDectorFactoryNew_InvalidSpec(t *testing.T) { |
| 47 |
assert := assert.New(t) |
| 48 |
m, err := New("localhost") |
| 49 |
assert.NotNil(err) |
| 50 |
assert.Nil(m) |
| 51 |
} |
| 52 |
|
| 53 |
func TestDectorFactoryNew_TrivialSpec(t *testing.T) { |
| 54 |
assert := assert.New(t) |
| 55 |
m, err := New("localhost:1") |
| 56 |
assert.NoError(err) |
| 57 |
assert.NotNil(m) |
| 58 |
assert.IsType(&Standalone{}, m) |
| 59 |
} |