diff options
Diffstat (limited to 'bus_test.go')
| -rw-r--r-- | bus_test.go | 60 |
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) +} |
