Skip to content

Commit 03f0960

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 See more about running examples in "Testable Examples in Go" [1] and testing package documentation [2]. 1. https://go.dev/blog/examples 2. https://pkg.go.dev/testing#hdr-Examples
1 parent 647609d commit 03f0960

File tree

2 files changed

+129
-113
lines changed

2 files changed

+129
-113
lines changed

README.md

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

@@ -548,118 +547,6 @@ func decodeTuple(d *msgpack.Decoder, v reflect.Value) error {
548547
* `User` - user name to log into Tarantool.
549548
* `Pass` - user password to log into Tarantool.
550549

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

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

queue/example_msgpack_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Setup queue module and start Tarantool instance before execution:
2+
// Terminal 1:
3+
// $ make deps
4+
// $ TT_LISTEN=3013 tarantool queue/config.lua
5+
//
6+
// Terminal 2:
7+
// $ cd queue
8+
// $ go test -v example_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_queueCustomMsgPackStructure demonstrates an operations like Put and
38+
// Take with queue and custom MsgPack structure.
39+
//
40+
// Features of the implementation:
41+
// - If you use connection timeout and call `TakeWithTimeout` with parameter
42+
// greater than the connection timeout then parameter reduced to it.
43+
// - If you use connection timeout and call `Take` then we return a error
44+
// if we can not take task from queue in a time equal to the connection timeout.
45+
func Example_queueCustomMsgPackStructure() {
46+
opts := tarantool.Opts{
47+
Reconnect: time.Second,
48+
Timeout: 2500 * time.Millisecond,
49+
MaxReconnects: 5,
50+
User: "test",
51+
Pass: "test",
52+
}
53+
conn, err := tarantool.Connect("127.0.0.1:3013", opts)
54+
if err != nil {
55+
log.Fatalf("connection: %s", err)
56+
return
57+
}
58+
defer conn.Close()
59+
60+
cfg := queue.Cfg{
61+
Temporary: true,
62+
IfNotExists: true,
63+
Kind: queue.FIFO,
64+
Opts: queue.Opts{
65+
Ttl: 10 * time.Second,
66+
Ttr: 5 * time.Second,
67+
Delay: 3 * time.Second,
68+
Pri: 1,
69+
},
70+
}
71+
72+
que := queue.New(conn, "test_queue")
73+
if err = que.Create(cfg); err != nil {
74+
log.Fatalf("queue create: %s", err)
75+
return
76+
}
77+
78+
// Put data
79+
task, err := que.Put("test_data")
80+
if err != nil {
81+
log.Fatalf("put task: %s", err)
82+
}
83+
fmt.Println("Task id is", task.Id())
84+
85+
// Take data
86+
task, err = que.Take() // Blocking operation
87+
if err != nil {
88+
log.Fatalf("take task: %s", err)
89+
}
90+
fmt.Println("Data is", task.Data())
91+
task.Ack()
92+
93+
// Take typed example
94+
putData := dummyData{}
95+
// Put data
96+
task, err = que.Put(&putData)
97+
if err != nil {
98+
log.Fatalf("put typed task: %s", err)
99+
}
100+
fmt.Println("Task id is ", task.Id())
101+
102+
takeData := dummyData{}
103+
// Take data
104+
task, err = que.TakeTyped(&takeData) // Blocking operation
105+
if err != nil {
106+
log.Fatalf("take take typed: %s", err)
107+
}
108+
fmt.Println("Data is ", takeData)
109+
// Same data
110+
fmt.Println("Data is ", task.Data())
111+
112+
task, err = que.Put([]int{1, 2, 3})
113+
task.Bury()
114+
115+
task, err = que.TakeTimeout(2 * time.Second)
116+
if task == nil {
117+
fmt.Println("Task is nil")
118+
}
119+
120+
que.Drop()
121+
122+
// Unordered output:
123+
// Task id is 0
124+
// Data is test_data
125+
// Task id is 0
126+
// Data is {false}
127+
// Data is &{false}
128+
// Task is nil
129+
}

0 commit comments

Comments
 (0)