github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/mocks/mock_publisher.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package mocks 19 20 import ( 21 "sync" 22 ) 23 24 // EventBusEntry represents the entry in publisher's history 25 type EventBusEntry struct { 26 Topic string 27 Event interface{} 28 } 29 30 // EventBus is a fake event bus. 31 type EventBus struct { 32 publishLast interface{} 33 publishHistory []EventBusEntry 34 lock sync.Mutex 35 } 36 37 // NewEventBus creates a new fake event bus. 38 func NewEventBus() *EventBus { 39 return &EventBus{ 40 publishHistory: make([]EventBusEntry, 0), 41 } 42 } 43 44 // Publish fakes publish. 45 func (mp *EventBus) Publish(topic string, event interface{}) { 46 mp.lock.Lock() 47 defer mp.lock.Unlock() 48 49 mp.publishHistory = append(mp.publishHistory, EventBusEntry{ 50 Topic: topic, 51 Event: event, 52 }) 53 mp.publishLast = event 54 } 55 56 // SubscribeAsync fakes async subsribe. 57 func (mp *EventBus) SubscribeAsync(topic string, fn interface{}) error { 58 return nil 59 } 60 61 // Subscribe fakes subscribe. 62 func (mp *EventBus) Subscribe(topic string, fn interface{}) error { 63 return nil 64 } 65 66 // SubscribeWithUID fakes subscribe. 67 func (mp *EventBus) SubscribeWithUID(topic, uid string, fn interface{}) error { 68 return nil 69 } 70 71 // Unsubscribe fakes unsubscribe. 72 func (mp *EventBus) Unsubscribe(topic string, fn interface{}) error { 73 return nil 74 } 75 76 // UnsubscribeWithUID fakes unsubscribe. 77 func (mp *EventBus) UnsubscribeWithUID(topic, uid string, fn interface{}) error { 78 return nil 79 } 80 81 // Pop pops the last event for assertions. 82 func (mp *EventBus) Pop() interface{} { 83 mp.lock.Lock() 84 defer mp.lock.Unlock() 85 result := mp.publishLast 86 mp.publishLast = nil 87 return result 88 } 89 90 // Clear clears the event history 91 func (mp *EventBus) Clear() { 92 mp.lock.Lock() 93 defer mp.lock.Unlock() 94 mp.publishHistory = make([]EventBusEntry, 0) 95 } 96 97 // GetEventHistory fetches the event history 98 func (mp *EventBus) GetEventHistory() []EventBusEntry { 99 mp.lock.Lock() 100 defer mp.lock.Unlock() 101 return mp.publishHistory 102 }