This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Download swagger.yaml from s3 and git
- Loading branch information
Showing
15 changed files
with
507 additions
and
7 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
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,37 @@ | ||
package async_test | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAsync(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Async Suite") | ||
} | ||
|
||
type Runner struct { | ||
Error error | ||
Count int | ||
Mutex sync.RWMutex | ||
} | ||
|
||
func (r *Runner) Run(ctx context.Context) error { | ||
r.Mutex.Lock() | ||
r.Count++ | ||
r.Mutex.Unlock() | ||
|
||
Expect(ctx).NotTo(BeNil()) | ||
return r.Error | ||
} | ||
|
||
func (r *Runner) Execution() int { | ||
r.Mutex.RLock() | ||
defer r.Mutex.RUnlock() | ||
|
||
return r.Count | ||
} |
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,69 @@ | ||
package async | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
// TaskFunc is the task's func | ||
type TaskFunc func(ctx context.Context) error | ||
|
||
// Task represents a task | ||
type Task struct { | ||
ctx context.Context | ||
cancel func() | ||
err error | ||
wg *sync.WaitGroup | ||
exec TaskFunc | ||
data []interface{} | ||
} | ||
|
||
// NewTask creates a new task | ||
func NewTask(fn TaskFunc, data ...interface{}) *Task { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
return &Task{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
wg: &sync.WaitGroup{}, | ||
exec: fn, | ||
data: data, | ||
} | ||
} | ||
|
||
// Data returns the tasks data | ||
func (t *Task) Data() interface{} { | ||
switch len(t.data) { | ||
case 0: | ||
return nil | ||
case 1: | ||
return t.data[0] | ||
default: | ||
return t.data | ||
} | ||
} | ||
|
||
// Run runs the task | ||
func (t *Task) Run() { | ||
t.wg.Add(1) | ||
|
||
go func() { | ||
if err := t.exec(t.ctx); err != nil { | ||
t.err = err | ||
} | ||
|
||
t.wg.Done() | ||
}() | ||
} | ||
|
||
// Stop stops the task | ||
func (t *Task) Stop() error { | ||
t.cancel() | ||
t.wg.Wait() | ||
return t.err | ||
} | ||
|
||
// Wait waits the task to be stopped | ||
func (t *Task) Wait() error { | ||
t.wg.Wait() | ||
return t.err | ||
} |
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,113 @@ | ||
package async_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/phogolabs/stride/async" | ||
) | ||
|
||
var _ = Describe("Task", func() { | ||
Describe("NewTask", func() { | ||
It("creates a task successfully", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
Expect(task).NotTo(BeNil()) | ||
}) | ||
}) | ||
|
||
Describe("Run", func() { | ||
It("starts a task successfully", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
task.Run() | ||
|
||
Eventually(runner.Execution).Should(Equal(1)) | ||
}) | ||
}) | ||
|
||
Describe("Data", func() { | ||
It("returns the associated data", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
Expect(task.Data()).To(Equal("root")) | ||
}) | ||
|
||
Context("when no data is provided", func() { | ||
It("returns nil", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run) | ||
Expect(task.Data()).To(BeNil()) | ||
}) | ||
}) | ||
|
||
Context("when more than one item is provided", func() { | ||
It("returns the associated data", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run, "root", "guest") | ||
Expect(task.Data()).To(HaveLen(2)) | ||
Expect(task.Data()).To(ContainElement("root")) | ||
Expect(task.Data()).To(ContainElement("guest")) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("Wait", func() { | ||
It("waits for the function successfully", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
task.Run() | ||
|
||
Expect(task.Wait()).To(Succeed()) | ||
Expect(runner.Count).To(Equal(1)) | ||
}) | ||
|
||
Context("when the runner fails", func() { | ||
It("returns an error", func() { | ||
runner := &Runner{ | ||
Error: fmt.Errorf("oh no"), | ||
} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
task.Run() | ||
|
||
Expect(task.Wait()).To(MatchError("oh no")) | ||
Expect(runner.Count).To(Equal(1)) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("Stop", func() { | ||
It("stops the function successfully", func() { | ||
runner := &Runner{} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
task.Run() | ||
|
||
Expect(task.Stop()).To(Succeed()) | ||
Expect(runner.Count).To(Equal(1)) | ||
}) | ||
}) | ||
|
||
Context("when the runner fails", func() { | ||
It("returns an error", func() { | ||
runner := &Runner{ | ||
Error: fmt.Errorf("oh no"), | ||
} | ||
|
||
task := async.NewTask(runner.Run, "root") | ||
task.Run() | ||
|
||
Expect(task.Stop()).To(MatchError("oh no")) | ||
Expect(runner.Count).To(Equal(1)) | ||
}) | ||
}) | ||
}) |
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,28 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/phogolabs/cli" | ||
"github.com/phogolabs/stride/torrent" | ||
) | ||
|
||
func get(ctx *cli.Context, key string) (string, error) { | ||
// get the spec async | ||
task, err := torrent.GetAsync(ctx.String(key)) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// make the task available within the app | ||
ctx.Metadata["task"] = task | ||
|
||
// wait the download to finish | ||
if err = task.Wait(); err != nil { | ||
// if there are some error stop | ||
return "", err | ||
} | ||
|
||
path := fmt.Sprintf("%v", task.Data()) | ||
return path, nil | ||
} |
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
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
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
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
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
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
Oops, something went wrong.