Skip to content

Commit

Permalink
Merge pull request #25 from donutloop/feat/example_tests
Browse files Browse the repository at this point in the history
Feat/example tests
  • Loading branch information
donutloop committed Sep 30, 2017
2 parents f7de654 + ae6ce38 commit c7711e2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
32 changes: 32 additions & 0 deletions concurrent/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package concurrent_test

import (
"fmt"
"github.com/donutloop/toolkit/concurrent"
"sync/atomic"
)

// Run concurrently your func() error
func ExampleRun() {

counter := int32(0)
errs := concurrent.Run(
func() error {
atomic.AddInt32(&counter, 40)
return nil
},
func() error {
atomic.AddInt32(&counter, 2)
return nil
},
)

if len(errs) > 0 {
for _, err := range errs {
fmt.Println(fmt.Sprintf("error: %v", err))
}
}

fmt.Println(counter)
// Output: 42
}
21 changes: 21 additions & 0 deletions event/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package event_test

import (
"fmt"
"github.com/donutloop/toolkit/event"
)

func ExampleHooks() {

hooks := new(event.Hooks)
hooks.Add(func() { fmt.Println("kernel request") })

errs := hooks.Fire()
if len(errs) > 0 {
for _, err := range errs {
fmt.Println(fmt.Sprintf("error: %v", err))
}
}

// Output: kernel request
}
20 changes: 20 additions & 0 deletions loop/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package loop_test

import (
"fmt"
"github.com/donutloop/toolkit/loop"
"time"
)

func ExampleLoop() {

loop.NewLooper(1*time.Second, func() error {
// do after one second things
return nil
})

// error and stop handling is missing for simplicity

fmt.Println("successfully")
// Output: successfully
}
23 changes: 23 additions & 0 deletions promise/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package promise_test

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

func ExamplePromise() {

done, errc := promise.Do(context.Background(), func(ctx context.Context) error {
fmt.Println("do things")
return nil
})

select {
case <-done:
case err := <-errc:
fmt.Println(fmt.Sprintf("error: %v", err))
}

// Output: do things
}

0 comments on commit c7711e2

Please sign in to comment.