watcher/mock: MockHandler

a mock event handler that can be primed with a series of expected
messages.

Useful for testing watcher
This commit is contained in:
Paul Stemmet 2022-12-09 12:31:06 +00:00
parent c72d92bbd2
commit 196bdde70f
Signed by: Paul Stemmet
GPG Key ID: EDEA539F594E7E75
1 changed files with 34 additions and 0 deletions

34
watcher/mock/mock.go Normal file
View File

@ -0,0 +1,34 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package mock
import (
"fmt"
"testing"
n "git.st8l.com/luxolus/kdnotify/schema/notify"
)
type MockHandler struct {
T *testing.T
Expected []n.VrrpMessage
HandlerFunc func(t *testing.T, expected, actual *n.VrrpMessage) bool
}
func (m *MockHandler) ProcessVrrp(actual n.VrrpMessage) error {
var expected *n.VrrpMessage
if len(m.Expected) > 1 {
expected = &m.Expected[0]
m.Expected = m.Expected[1:]
}
if !m.HandlerFunc(m.T, expected, &actual) {
return fmt.Errorf("MockHandler.HandlerFunc test fail")
}
return nil
}