-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
129 lines (107 loc) · 3.88 KB
/
example_test.go
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
package queue_test
import (
"fmt"
"github.com/jakewins/4fq/pkg/queue"
)
// This variant is safe for multiple producers and multiple consumers,
// making it the most general of the four queues - if you're unsure which
// one to use, this is the safe choice.
// To be safe for multiple producers and consumers, this version of the
// queue has to take some extra precautions. If you know for a fact that
// there will be, for instance, just one consumer or just one producer,
// you may consider looking at one of the specialized options.
func ExampleMultiProducerMultiConsumer() {
q, err := queue.NewSingleProducerSingleConsumer(queue.Options{})
if err != nil {
// May happen if options are invalid, for instance
panic(err)
}
// Put something on the queue
// 1: Get a queue slot to stick our value in
slot, err := q.NextFree()
slot.Val = "Hello, world!"
// 2: Publish the slot
q.Publish(slot)
// Read from the queue
// Drain reads in bulk, blocking until at least one message is available,
q.Drain(func(slot *queue.Slot) {
fmt.Printf("Received: %s\n", slot.Val)
})
// Output:
// Received: Hello, world!
}
func ExampleMultiProducerSingleConsumer() {
// Create an MPSC Queue
q, err := queue.NewMultiProducerSingleConsumer(queue.Options{})
if err != nil {
// May happen if options are invalid, for instance
panic(err)
}
// Put something on the queue
// 1: Get a queue slot to stick our value in
slot, err := q.NextFree()
slot.Val = "Hello, world!"
// 2: Publish the slot
q.Publish(slot)
// Read from the queue
// Drain reads in bulk, blocking until at least one message is available,
q.Drain(func(slot *queue.Slot) {
fmt.Printf("Received: %s\n", slot.Val)
})
// Output:
// Received: Hello, world!
}
// If you promise to just have one go routine publish to the queue, and only
// one reading from it, you can use this variant which improves latency by removing concurrency
// checks that are only needed if there are multiple publishers and subscribers
func ExampleSingleProducerSingleConsumer() {
// Create an SPSC Queue - note that there is no safety net: If you
// have multiple go routines publishing to or reading from this simultaneously bad things
// will happen. Use the MultiProducer variant if you're uncertain,
// and/or use the go test -race flag to test your programs safety.
q, err := queue.NewSingleProducerSingleConsumer(queue.Options{})
if err != nil {
// May happen if options are invalid, for instance
panic(err)
}
// Put something on the queue
// 1: Get a queue slot to stick our value in
slot, err := q.NextFree()
slot.Val = "Hello, world!"
// 2: Publish the slot
q.Publish(slot)
// Read from the queue
// Drain reads in bulk, blocking until at least one message is available,
q.Drain(func(slot *queue.Slot) {
fmt.Printf("Received: %s\n", slot.Val)
})
// Output:
// Received: Hello, world!
}
// If you promise to just have one go routine publish to the queue,
// you can use this variant which improves latency by removing concurrency
// checks that are only needed if there are multiple publishers
func ExampleSingleProducerMultiConsumer() {
// Create an SPMC Queue - note that there is no safety net: If you
// have multiple go routines publishing to this simultaneously bad things
// will happen. Use the MultiProducer variant if you're uncertain,
// and/or use the go test -race flag to test your programs safety.
q, err := queue.NewSingleProducerMultiConsumer(queue.Options{})
if err != nil {
// May happen if options are invalid, for instance
panic(err)
}
// Put something on the queue
// 1: Get a queue slot to stick our value in
slot, err := q.NextFree()
slot.Val = "Hello, world!"
// 2: Publish the slot
q.Publish(slot)
// Read from the queue
// Drain reads in bulk, blocking until at least one message is available,
q.Drain(func(slot *queue.Slot) {
fmt.Printf("Received: %s\n", slot.Val)
})
// Output:
// Received: Hello, world!
}