kdnotify/fifo/mock/events.go

63 lines
1.1 KiB
Go

/*
* 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
}