Skip to content

Commit 91a07ab

Browse files
committed
readme: move queue example to tests
- make example executable with adding reference output - add description of steps how to run example manually - update comments for methods in queue.go See more about running examples in "Testable Examples in Go" [1] and testing package documentation [2]. Part of #123 1. https://go.dev/blog/examples 2. https://pkg.go.dev/testing#hdr-Examples
1 parent 8b1bd33 commit 91a07ab

File tree

3 files changed

+131
-116
lines changed

3 files changed

+131
-116
lines changed

README.md

Lines changed: 0 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ faster than other packages according to public benchmarks.
3030
* [Schema](#schema)
3131
* [Custom (un)packing and typed selects and function calls](#custom-unpacking-and-typed-selects-and-function-calls)
3232
* [Options](#options)
33-
* [Working with queue](#working-with-queue)
3433
* [Tests](#tests)
3534
* [Alternative connectors](#alternative-connectors)
3635

@@ -551,118 +550,6 @@ func decodeTuple(d *msgpack.Decoder, v reflect.Value) error {
551550
* `User` - user name to log into Tarantool.
552551
* `Pass` - user password to log into Tarantool.
553552

554-
## Working with queue
555-
```go
556-
package main
557-
import (
558-
"gopkg.in/vmihailenco/msgpack.v2"
559-
"github.com/tarantool/go-tarantool"
560-
"github.com/tarantool/go-tarantool/queue"
561-
"time"
562-
"fmt"
563-
"log"
564-
)
565-
566-
type customData struct{
567-
Dummy bool
568-
}
569-
570-
func (c *customData) DecodeMsgpack(d *msgpack.Decoder) error {
571-
var err error
572-
if c.Dummy, err = d.DecodeBool(); err != nil {
573-
return err
574-
}
575-
return nil
576-
}
577-
578-
func (c *customData) EncodeMsgpack(e *msgpack.Encoder) error {
579-
return e.EncodeBool(c.Dummy)
580-
}
581-
582-
func main() {
583-
opts := tarantool.Opts{
584-
Timeout: time.Second,
585-
Reconnect: time.Second,
586-
MaxReconnects: 5,
587-
User: "user",
588-
Pass: "pass",
589-
// ...
590-
}
591-
conn, err := tarantool.Connect("127.0.0.1:3301", opts)
592-
593-
if err != nil {
594-
log.Fatalf("connection: %s", err)
595-
return
596-
}
597-
598-
cfg := queue.Cfg{
599-
Temporary: true,
600-
IfNotExists: true,
601-
Kind: queue.FIFO,
602-
Opts: queue.Opts{
603-
Ttl: 10 * time.Second,
604-
Ttr: 5 * time.Second,
605-
Delay: 3 * time.Second,
606-
Pri: 1,
607-
},
608-
}
609-
610-
que := queue.New(conn, "test_queue")
611-
if err = que.Create(cfg); err != nil {
612-
log.Fatalf("queue create: %s", err)
613-
return
614-
}
615-
616-
// put data
617-
task, err := que.Put("test_data")
618-
if err != nil {
619-
log.Fatalf("put task: %s", err)
620-
}
621-
fmt.Println("Task id is", task.Id())
622-
623-
// take data
624-
task, err = que.Take() //blocking operation
625-
if err != nil {
626-
log.Fatalf("take task: %s", err)
627-
}
628-
fmt.Println("Data is", task.Data())
629-
task.Ack()
630-
631-
// take typed example
632-
putData := customData{}
633-
// put data
634-
task, err = que.Put(&putData)
635-
if err != nil {
636-
log.Fatalf("put typed task: %s", err)
637-
}
638-
fmt.Println("Task id is ", task.Id())
639-
640-
takeData := customData{}
641-
//take data
642-
task, err = que.TakeTyped(&takeData) //blocking operation
643-
if err != nil {
644-
log.Fatalf("take take typed: %s", err)
645-
}
646-
fmt.Println("Data is ", takeData)
647-
// same data
648-
fmt.Println("Data is ", task.Data())
649-
650-
task, err = que.Put([]int{1, 2, 3})
651-
task.Bury()
652-
653-
task, err = que.TakeTimeout(2 * time.Second)
654-
if task == nil {
655-
fmt.Println("Task is nil")
656-
}
657-
658-
que.Drop()
659-
}
660-
```
661-
Features of the implementation:
662-
663-
- If you use connection timeout and call `TakeWithTimeout` with parameter greater than the connection timeout then parameter reduced to it
664-
- If you use connection timeout and call `Take` then we return a error if we can not take task from queue in a time equal to the connection timeout
665-
666553
## Multi connections
667554

668555
You can use multiple connections config with tarantool/multi.

queue/example_msgpack_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Setup queue module and start Tarantool instance before execution:
2+
// Terminal 1:
3+
// $ make deps
4+
// $ TEST_TNT_LISTEN=3013 tarantool queue/config.lua
5+
//
6+
// Terminal 2:
7+
// $ cd queue
8+
// $ go test -v example_msgpack_test.go
9+
package queue_test
10+
11+
import (
12+
"fmt"
13+
"time"
14+
15+
"github.com/tarantool/go-tarantool"
16+
"github.com/tarantool/go-tarantool/queue"
17+
"gopkg.in/vmihailenco/msgpack.v2"
18+
"log"
19+
)
20+
21+
type dummyData struct {
22+
Dummy bool
23+
}
24+
25+
func (c *dummyData) DecodeMsgpack(d *msgpack.Decoder) error {
26+
var err error
27+
if c.Dummy, err = d.DecodeBool(); err != nil {
28+
return err
29+
}
30+
return nil
31+
}
32+
33+
func (c *dummyData) EncodeMsgpack(e *msgpack.Encoder) error {
34+
return e.EncodeBool(c.Dummy)
35+
}
36+
37+
// Example demonstrates an operations like Put and Take with queue and custom
38+
// MsgPack structure.
39+
func Example_simpleQueueCustomMsgPack() {
40+
opts := tarantool.Opts{
41+
Reconnect: time.Second,
42+
Timeout: 2500 * time.Millisecond,
43+
MaxReconnects: 5,
44+
User: "test",
45+
Pass: "test",
46+
}
47+
conn, err := tarantool.Connect("127.0.0.1:3013", opts)
48+
if err != nil {
49+
log.Fatalf("connection: %s", err)
50+
return
51+
}
52+
defer conn.Close()
53+
54+
cfg := queue.Cfg{
55+
Temporary: true,
56+
IfNotExists: true,
57+
Kind: queue.FIFO,
58+
Opts: queue.Opts{
59+
Ttl: 10 * time.Second,
60+
Ttr: 5 * time.Second,
61+
Delay: 3 * time.Second,
62+
Pri: 1,
63+
},
64+
}
65+
66+
que := queue.New(conn, "test_queue_msgpack")
67+
if err = que.Create(cfg); err != nil {
68+
log.Fatalf("queue create: %s", err)
69+
return
70+
}
71+
72+
// Put data
73+
task, err := que.Put("test_data")
74+
if err != nil {
75+
log.Fatalf("put task: %s", err)
76+
}
77+
fmt.Println("Task id is", task.Id())
78+
79+
// Take data
80+
task, err = que.Take() // Blocking operation
81+
if err != nil {
82+
log.Fatalf("take task: %s", err)
83+
}
84+
fmt.Println("Data is", task.Data())
85+
task.Ack()
86+
87+
// Take typed example
88+
putData := dummyData{}
89+
// Put data
90+
task, err = que.Put(&putData)
91+
if err != nil {
92+
log.Fatalf("put typed task: %s", err)
93+
}
94+
fmt.Println("Task id is ", task.Id())
95+
96+
takeData := dummyData{}
97+
// Take data
98+
task, err = que.TakeTyped(&takeData) // Blocking operation
99+
if err != nil {
100+
log.Fatalf("take take typed: %s", err)
101+
}
102+
fmt.Println("Data is ", takeData)
103+
// Same data
104+
fmt.Println("Data is ", task.Data())
105+
106+
task, err = que.Put([]int{1, 2, 3})
107+
task.Bury()
108+
109+
task, err = que.TakeTimeout(2 * time.Second)
110+
if task == nil {
111+
fmt.Println("Task is nil")
112+
}
113+
114+
que.Drop()
115+
116+
// Unordered output:
117+
// Task id is 0
118+
// Data is test_data
119+
// Task id is 0
120+
// Data is {false}
121+
// Data is &{false}
122+
// Task is nil
123+
}

queue/queue.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,23 @@ type Queue interface {
3737
// Take takes 'ready' task from a tube and marks it as 'in progress'
3838
// Note: if connection has a request Timeout, then 0.9 * connection.Timeout is
3939
// used as a timeout.
40+
// If you use a connection timeout and we can not take task from queue in
41+
// a time equal to the connection timeout after calling `Take` then we
42+
// return an error.
4043
Take() (*Task, error)
41-
// TakeWithTimout takes 'ready' task from a tube and marks it as "in progress",
44+
// TakeTimeout takes 'ready' task from a tube and marks it as "in progress",
4245
// or it is timeouted after "timeout" period.
4346
// Note: if connection has a request Timeout, and conn.Timeout * 0.9 < timeout
4447
// then timeout = conn.Timeout*0.9
48+
// If you use connection timeout and call `TakeTimeout` with parameter
49+
// greater than the connection timeout then parameter reduced to it.
4550
TakeTimeout(timeout time.Duration) (*Task, error)
46-
// Take takes 'ready' task from a tube and marks it as 'in progress'
51+
// TakeTyped takes 'ready' task from a tube and marks it as 'in progress'
4752
// Note: if connection has a request Timeout, then 0.9 * connection.Timeout is
4853
// used as a timeout.
4954
// Data will be unpacked to result
5055
TakeTyped(interface{}) (*Task, error)
51-
// TakeWithTimout takes 'ready' task from a tube and marks it as "in progress",
56+
// TakeTypedTimeout takes 'ready' task from a tube and marks it as "in progress",
5257
// or it is timeouted after "timeout" period.
5358
// Note: if connection has a request Timeout, and conn.Timeout * 0.9 < timeout
5459
// then timeout = conn.Timeout*0.9

0 commit comments

Comments
 (0)