aboutsummaryrefslogtreecommitdiff
path: root/bus_test.go
diff options
context:
space:
mode:
author2026-02-05 14:35:00 +0300
committer2026-02-05 14:35:00 +0300
commit641a95063946bf7f4b8fdd1e938a917bc3f75cb2 (patch)
treed19a6b92845de37e66b57b305fbcaf9f4816ffe9 /bus_test.go
downloadfqueue-master.tar.gz
fqueue-master.tar.bz2
fqueue-master.tar.xz
fqueue-master.zip
initialHEADmaster
Diffstat (limited to '')
-rw-r--r--bus_test.go60
1 files changed, 60 insertions, 0 deletions
diff --git a/bus_test.go b/bus_test.go
new file mode 100644
index 0000000..ca3a664
--- /dev/null
+++ b/bus_test.go
@@ -0,0 +1,60 @@
+package fqueue_test
+
+import (
+ "context"
+ "fmt"
+ "os"
+ "strconv"
+ "time"
+
+ "go.neonxp.ru/fqueue"
+)
+
+func Example() {
+ tasksDir, err := os.MkdirTemp(os.TempDir(), "tasks_*")
+ if err != nil {
+ panic(err)
+ }
+ defer os.RemoveAll(tasksDir)
+
+ bus, err := fqueue.New(tasksDir)
+ if err != nil {
+ panic(err)
+ }
+
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
+ defer cancel()
+
+ for i := range 5 {
+ t := Task("Task #" + strconv.Itoa(i))
+ id, err := bus.Publish(ctx, t.Encode())
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println("Task id=", id, "published with text", t)
+ }
+
+ for t, err := range bus.NextTask(ctx) {
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println("Got task", string(t))
+ }
+ // Output:
+ // Task id= 1 published with text Task #0
+ // Task id= 2 published with text Task #1
+ // Task id= 3 published with text Task #2
+ // Task id= 4 published with text Task #3
+ // Task id= 5 published with text Task #4
+ // Got task Task #0
+ // Got task Task #1
+ // Got task Task #2
+ // Got task Task #3
+ // Got task Task #4
+}
+
+type Task string
+
+func (t Task) Encode() []byte {
+ return []byte(t)
+}