github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/broker/memory/memory_test.go (about) 1 package memory 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/volts-dev/volts/broker" 8 ) 9 10 func TestMemoryBroker(t *testing.T) { 11 b := New() 12 13 if err := b.Start(); err != nil { 14 t.Fatalf("Unexpected connect error %v", err) 15 } 16 17 topic := "test" 18 count := 10 19 20 fn := func(p broker.IEvent) error { 21 return nil 22 } 23 24 sub, err := b.Subscribe(topic, fn) 25 if err != nil { 26 t.Fatalf("Unexpected error subscribing %v", err) 27 } 28 29 for i := 0; i < count; i++ { 30 message := &broker.Message{ 31 Header: map[string]string{ 32 "foo": "bar", 33 "id": fmt.Sprintf("%d", i), 34 }, 35 Body: []byte(`hello world`), 36 } 37 38 if err := b.Publish(topic, message); err != nil { 39 t.Fatalf("Unexpected error publishing %d", i) 40 } 41 } 42 43 if err := sub.Unsubscribe(); err != nil { 44 t.Fatalf("Unexpected error unsubscribing from %s: %v", topic, err) 45 } 46 47 if err := b.Close(); err != nil { 48 t.Fatalf("Unexpected connect error %v", err) 49 } 50 }