syncbus/example_test.go

47 lines
660 B
Go
Raw Permalink Normal View History

2018-03-18 20:42:20 +01:00
package syncbus_test
import (
"fmt"
"sync"
"time"
2024-12-17 18:28:27 +01:00
"code.squareroundforest.org/arpio/syncbus"
2018-03-18 20:42:20 +01:00
)
type Server struct {
resource int
mx sync.Mutex
testBus *syncbus.SyncBus
}
func (s *Server) AsyncInit() {
go func() {
s.mx.Lock()
defer s.mx.Unlock()
s.resource = 42
s.testBus.Signal("initialized")
}()
}
func (s *Server) Resource() int {
s.mx.Lock()
defer s.mx.Unlock()
return s.resource
}
func Example() {
2018-03-18 21:05:53 +01:00
bus := syncbus.New(120 * time.Millisecond)
2018-03-18 20:42:20 +01:00
2018-03-18 21:05:53 +01:00
s := &Server{}
s.testBus = bus
2018-03-18 20:42:20 +01:00
s.AsyncInit()
2018-03-18 21:05:53 +01:00
if err := bus.Wait("initialized"); err != nil {
2018-03-18 20:42:20 +01:00
fmt.Println("failed:", err)
}
fmt.Println(s.Resource())
// Output:
// 42
}