-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from donutloop/feat/example_tests
Feat/example tests
- Loading branch information
Showing
4 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |