From 06f1ce010a966ce378691bdd2a2c007a2974c7d6 Mon Sep 17 00:00:00 2001 From: Marvin Preuss Date: Sat, 25 Sep 2021 14:39:16 +0200 Subject: [PATCH] test: adds a example --- .goreleaser.yml | 1 + example_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 example_test.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 5eb8f34..19be8a2 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -10,6 +10,7 @@ changelog: - "^build:" - "^ci:" - "^repo:" + - "^test:" builds: - skip: true diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..56ca424 --- /dev/null +++ b/example_test.go @@ -0,0 +1,49 @@ +package workgroups_test + +import ( + "context" + "fmt" + "log" + "runtime" + + "go.xsfx.dev/workgroups" +) + +func Example() { + d, ctx := workgroups.NewDispatcher( + context.Background(), + runtime.GOMAXPROCS(0), // This starts as much worker as maximal processes are allowed for go. + ) + + work := func(ctx context.Context) error { + // Check if context already expired. + // Return if its the case, else just go forward. + select { + case <-ctx.Done(): + return fmt.Errorf("got error from context: %w", ctx.Err()) + default: + } + + // Some wannebe work... printing something. + fmt.Print("hello world from work") + + return nil + } + + // Starting up the workers. + d.Start(ctx) + + // Feeding the workers some work. + d.Append(work) + + // Closing the channel for work. + d.Close() + + // Waiting to finnish everything. + if err := d.Wait(); err != nil { + log.Fatal(err) + } + + // Output: + // hello world from work +}