File: pubsub_test.go

package info (click to toggle)
golang-github-igm-pubsub 1.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 72 kB
  • sloc: makefile: 2
file content (152 lines) | stat: -rw-r--r-- 2,677 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package pubsub_test

import (
	"github.com/igm/pubsub"
	"fmt"
	"testing"
	"time"
)

func TestSubReader(t *testing.T) {
	pub := pubsub.Publisher{}
	r, last := pub.SubReader()
	if last != nil {
		t.Fatal("last message shuold be nil")
	}
	pub.Publish("msg 1")
	pub.Publish("msg 2")
	if r.Read() != "msg 1" {
		t.Fatal("incorrect message received")
	}
	if r.Read() != "msg 2" {
		t.Fatal("incorrect message received")
	}
}

func TestMultipleSubReaders(t *testing.T) {
	pub := pubsub.Publisher{}
	r, last := pub.SubReader()
	if last != nil {
		t.Fatal("last message shuold be nil")
	}
	pub.Publish("msg 1")

	r2, last2 := pub.SubReader()
	if last2 != "msg 1" {
		t.Fatal("last message shuold be nil")
	}

	pub.Publish("msg 2")
	if r.Read() != "msg 1" {
		t.Fatal("incorrect message received")
	}
	if r.Read() != "msg 2" {
		t.Fatal("incorrect message received")
	}
	if r2.Read() != "msg 2" {
		t.Fatal("incorrect message received")
	}
}

func TestSubChannel(t *testing.T) {
	pub := pubsub.Publisher{}
	ch, last := pub.SubChannel(nil)
	if last != nil {
		t.Fatal("last message shuold be nil")
	}
	pub.Publish("msg 1")
	pub.Publish("msg 2")
	pub.Publish(nil)

	if "msg 1" != <-ch {
		t.Fatal("incorrect message received")
	}
	if "msg 2" != <-ch {
		t.Fatal("incorrect message received")
	}
	if nil != <-ch {
		t.Fatal("incorrect message received")
	}
	if _, ok := <-ch; ok {
		t.Fatal("channel should be closed")
	}
}

func ExamplePublisher() {
	pub := pubsub.Publisher{}
	ch, _ := pub.SubChannel(nil)
	pub.Publish("msg 1")
	pub.Publish("msg 2")
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	// Output:
	// msg 1
	// msg 2
}

func ExamplePublisher_SubChannel() {
	pub := new(pubsub.Publisher)
	ch, _ := pub.SubChannel("close")
	pub.Publish("msg 1")
	pub.Publish("msg 2")
	pub.Publish("close")

	for msg := range ch {
		fmt.Println(msg)
	}
	// Output:
	// msg 1
	// msg 2
	// close
}

func ExamplePublisher_SubReader() {
	type Timer struct {
		pubsub.Publisher
	}

	timer := new(Timer)

	go func() {
		for /*...*/ {
			time.Sleep(time.Second)
			timer.Publish(time.Now())
			// ...
		}
	}()

	reader, _ := timer.SubReader()
	for {
		fmt.Println(reader.Read())
	}
}

func ExamplePublisher_composition() {
	type StockExchange struct {
		pubsub.Publisher
	}
	se := new(StockExchange)

	ch, _ := se.SubChannel(nil)
	se.Publish("msg 1")
	se.Publish("msg 2")
	fmt.Println(<-ch)
	fmt.Println(<-ch)
	// Output:
	// msg 1
	// msg 2
}

func BenchmarkPubSub(b *testing.B) {
	pub := new(pubsub.Publisher)
	r1, _ := pub.SubReader()
	r2, _ := pub.SubReader()
	for i := 0; i < b.N; i++ {
		pub.Publish(i)
		v1 := r1.Read()
		v2 := r2.Read()
		if v1 != v2 || v1 != i {
			b.Fatal("incorrect value")
		}
	}
}