From 1812c93f254db69dd995d1113a033087c16e7559 Mon Sep 17 00:00:00 2001 From: Marcel Edmund Franke Date: Sat, 30 Sep 2017 18:09:04 +0200 Subject: [PATCH 1/3] - Added: Example test for retry --- retry/doc_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 retry/doc_test.go diff --git a/retry/doc_test.go b/retry/doc_test.go new file mode 100644 index 0000000..6ae3cd2 --- /dev/null +++ b/retry/doc_test.go @@ -0,0 +1,21 @@ +package retry_test + +import ( + "context" + "fmt" + "github.com/donutloop/toolkit/retry" +) + +func ExampleRetrier() { + r := retry.NewRetrier() + err := r.Retry(context.Background(), func() (bool, error) { + fmt.Println("fire request") + return true, nil + }) + + if err != nil { + fmt.Println(fmt.Sprintf("error: (%v)", err)) + } + + // Output: fire request +} From ef05d25cb12d48b7379b4edd11e3316b5f37ac1e Mon Sep 17 00:00:00 2001 From: Marcel Edmund Franke Date: Sat, 30 Sep 2017 18:13:45 +0200 Subject: [PATCH 2/3] - Added: Example test for schedule --- schedule/doc_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 schedule/doc_test.go diff --git a/schedule/doc_test.go b/schedule/doc_test.go new file mode 100644 index 0000000..04e5c9b --- /dev/null +++ b/schedule/doc_test.go @@ -0,0 +1,25 @@ +package schedule_test + +import ( + "github.com/donutloop/toolkit/schedule" + "context" + "fmt" +) + +func ExampleFIFOScheduler() { + + s := schedule.NewFIFOScheduler() + defer s.Stop() + + job := func(ctx context.Context) { + fmt.Println("create db entry") + } + + if err := s.Schedule(job); err != nil { + fmt.Println(fmt.Sprintf("error: (%v)", err)) + } + + s.WaitFinish(1) + + // Output: create db entry +} From e48a76c6faae5725f34e7b171d4c164ade23e01c Mon Sep 17 00:00:00 2001 From: Marcel Edmund Franke Date: Sat, 30 Sep 2017 18:20:14 +0200 Subject: [PATCH 3/3] - Added: example test for singleton --- schedule/doc_test.go | 2 +- singleton/README.md | 4 ++-- singleton/doc_test.go | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 singleton/doc_test.go diff --git a/schedule/doc_test.go b/schedule/doc_test.go index 04e5c9b..4ca72ff 100644 --- a/schedule/doc_test.go +++ b/schedule/doc_test.go @@ -1,9 +1,9 @@ package schedule_test import ( - "github.com/donutloop/toolkit/schedule" "context" "fmt" + "github.com/donutloop/toolkit/schedule" ) func ExampleFIFOScheduler() { diff --git a/singleton/README.md b/singleton/README.md index b42b3c1..55840aa 100644 --- a/singleton/README.md +++ b/singleton/README.md @@ -16,8 +16,8 @@ type config struct { Port int } -var configSingleton = NewSingleton(func() (interface{}, error) { - return &config{Addr:"localhost", Port:80,}, nil +var configSingleton singleton.NewSingleton = singleton.NewSingleton(func() (interface{}, error) { + return &config{Addr:"localhost", Port:80}, nil }) func Config() (*config, error) { diff --git a/singleton/doc_test.go b/singleton/doc_test.go new file mode 100644 index 0000000..30b92c2 --- /dev/null +++ b/singleton/doc_test.go @@ -0,0 +1,36 @@ +package singleton_test + +import ( + "fmt" + "github.com/donutloop/toolkit/singleton" +) + +func ExampleSingleton() { + + type config struct { + Addr string + Port int + } + + configSingleton := singleton.NewSingleton(func() (interface{}, error) { + return &config{Addr: "localhost", Port: 80}, nil + }) + + configFunc := func() (*config, error) { + s, err := configSingleton.Get() + if err != nil { + return nil, err + } + return s.(*config), nil + } + + configFunc() + + c, err := configFunc() + if err != nil { + fmt.Println(fmt.Sprintf("error: (%v)", err)) + } + + fmt.Println(fmt.Sprintf("%#v", c)) + // Output: &singleton_test.config{Addr:"localhost", Port:80} +}