Skip to content

Commit

Permalink
Merge pull request #26 from donutloop/feat/example_tests
Browse files Browse the repository at this point in the history
Feat/example tests
  • Loading branch information
donutloop authored Sep 30, 2017
2 parents c7711e2 + e48a76c commit 24010c9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
21 changes: 21 additions & 0 deletions retry/doc_test.go
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 25 additions & 0 deletions schedule/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package schedule_test

import (
"context"
"fmt"
"github.com/donutloop/toolkit/schedule"
)

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
}
4 changes: 2 additions & 2 deletions singleton/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
36 changes: 36 additions & 0 deletions singleton/doc_test.go
Original file line number Diff line number Diff line change
@@ -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}
}

0 comments on commit 24010c9

Please sign in to comment.