Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-lz committed Apr 30, 2024
1 parent 64600ee commit d70837f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 38 deletions.
70 changes: 37 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,42 @@ import (
)

func main() {
agent := gopubsub.NewAgent()
defer agent.Close()

// Subscribe to a topic
sub,_ := agent.Subscribe("foo")
sub2,cancel := agent.Subscribe("foo")
defer agent.Unsubscribe(sub2)
sub3 := agent.Subscribe("foo")
defer agent.Unsubscribe(sub3)

var wg = &sync.WaitGroup{}

wg.Add(3)
go func() {
defer wg.Done()
fmt.Println(<-sub.Msg)
}()

go func() {
defer wg.Done()
fmt.Println(<-sub2.Msg)
}()

go func() {
defer wg.Done()
fmt.Println(<-sub3.Msg)
}()


// Publish a message to the topic
go agent.Publish("foo", "hello world")

wg.Wait()
// channel to publish messages to
// Create a new agent
agent := gopubsub.NewAgent()
defer agent.Close()

// Subscribe to a topic
sub, _ := agent.Subscribe("foo")
sub2, cancel := agent.Subscribe("foo")
defer cancel(agent, sub2) // remember to unsubscribe !
sub3, cancel := agent.Subscribe("foo")
defer cancel(agent, sub3)

// Publish a message to the topic
var wg = &sync.WaitGroup{}

wg.Add(3)
go func() {
defer wg.Done()
fmt.Println(<-sub.Msg)
}()

go func() {
defer wg.Done()
fmt.Println(<-sub2.Msg)
}()

go func() {
defer wg.Done()
fmt.Println(<-sub3.Msg)
}()

go agent.Publish("foo", "hello world")

wg.Wait()

agent.Unsubscribe(sub)
fmt.Println("subscrip closed suber")
}
```
11 changes: 6 additions & 5 deletions test/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import (
func TestChanSub(t *testing.T) {
// channel to publish messages to
// Create a new agent

agent := gopubsub.NewAgent()
defer agent.Close()

// Subscribe to a topic
sub, _ := agent.Subscribe("foo")
sub2, cancel := agent.Subscribe("foo")
defer cancel(agent, sub2) // remember to unsubscribe !
defer cancel(agent, sub2) // remember to unsubscribe !
sub3, cancel := agent.Subscribe("foo")
defer cancel(agent, sub3)

Expand Down Expand Up @@ -54,9 +53,11 @@ func TestChanSub(t *testing.T) {
fmt.Println("channel is closed")
}

suber4,cancel := agent.Subscribe("foo2")
defer cancel(agent,suber4)
suber4, cancel := agent.Subscribe("foo2")
defer cancel(agent, suber4)
for _ = range 11 {
agent.Publish("foo2","hello")
if fail := agent.Publish("foo2", "hello"); fail != nil {
panic(fail)
}
}
}

0 comments on commit d70837f

Please sign in to comment.