From 196bdde70fc13e2941e87164a1ed55b8b9fd82a1 Mon Sep 17 00:00:00 2001 From: Bazaah Date: Fri, 9 Dec 2022 12:31:06 +0000 Subject: [PATCH] watcher/mock: MockHandler a mock event handler that can be primed with a series of expected messages. Useful for testing watcher --- watcher/mock/mock.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 watcher/mock/mock.go diff --git a/watcher/mock/mock.go b/watcher/mock/mock.go new file mode 100644 index 0000000..9c2f9e4 --- /dev/null +++ b/watcher/mock/mock.go @@ -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 +}