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 +}