From 6ab808c6f5e0bbfd3573c071d0ac34ce233c1e2e Mon Sep 17 00:00:00 2001 From: Bazaah Date: Fri, 9 Dec 2022 12:10:32 +0000 Subject: [PATCH] fifo/mock: MockEvents Mock producer which implements fifo.EventChannel. Useful elsewhere in the code, and also to kdnotify/fifo users. --- fifo/mock/events.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 fifo/mock/events.go diff --git a/fifo/mock/events.go b/fifo/mock/events.go new file mode 100644 index 0000000..a884435 --- /dev/null +++ b/fifo/mock/events.go @@ -0,0 +1,62 @@ +/* + * 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 ( + "git.st8l.com/luxolus/kdnotify/fifo" + n "git.st8l.com/luxolus/kdnotify/schema/notify" +) + +type MockEvents struct { + Events []n.VrrpMessage + Cursor int +} + +func NewMockEvents(msgs ...n.VrrpMessage) MockEvents { + return MockEvents{ + Events: msgs, + } +} + +func (m *MockEvents) Next() (n.VrrpMessage, error) { + if m.Done() { + return n.VrrpMessage{}, fifo.EClosed + } + + return m.Events[m.increment()], nil +} + +func (m *MockEvents) Done() bool { + return m.check(0) +} + +func (m *MockEvents) Current() *n.VrrpMessage { + if len(m.Events) == 0 { + return nil + } + + return &m.Events[m.Cursor] +} + +func (m *MockEvents) Peak() *n.VrrpMessage { + if !m.check(1) { + return nil + } + + return &m.Events[m.Cursor+1] +} + +func (m *MockEvents) check(pos int) bool { + return len(m.Events) == 0 || len(m.Events) == m.Cursor+1+pos +} + +func (m *MockEvents) increment() int { + i := m.Cursor + m.Cursor++ + + return i +}