fifo/mock: MockEvents
Mock producer which implements fifo.EventChannel. Useful elsewhere in the code, and also to kdnotify/fifo users.
This commit is contained in:
parent
65660e3286
commit
6ab808c6f5
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue