From 19f91142719a73cb057010920fba2de96dab5157 Mon Sep 17 00:00:00 2001 From: Mat Ryer Date: Mon, 5 Feb 2018 15:01:01 +0000 Subject: [PATCH] removed old package --- x/boxutil/doc.go | 2 - x/boxutil/info.go | 15 ------- x/boxutil/status.go | 70 ----------------------------- x/boxutil/status_test.go | 95 ---------------------------------------- 4 files changed, 182 deletions(-) delete mode 100644 x/boxutil/doc.go delete mode 100644 x/boxutil/info.go delete mode 100644 x/boxutil/status.go delete mode 100644 x/boxutil/status_test.go diff --git a/x/boxutil/doc.go b/x/boxutil/doc.go deleted file mode 100644 index ed1eaa7..0000000 --- a/x/boxutil/doc.go +++ /dev/null @@ -1,2 +0,0 @@ -// Package boxutil is DEPCREATED and you should use github.com/machinebox/sdk-go/boxutil instead. -package boxutil diff --git a/x/boxutil/info.go b/x/boxutil/info.go deleted file mode 100644 index 61935ca..0000000 --- a/x/boxutil/info.go +++ /dev/null @@ -1,15 +0,0 @@ -package boxutil - -// Box represents a box client capable of returning -// Info. -type Box interface { - Info() (*Info, error) -} - -// Info describes box information. -type Info struct { - Name string - Version int - Build string - Status string -} diff --git a/x/boxutil/status.go b/x/boxutil/status.go deleted file mode 100644 index 96a0ad8..0000000 --- a/x/boxutil/status.go +++ /dev/null @@ -1,70 +0,0 @@ -package boxutil - -import ( - "context" - "errors" - "log" - "time" -) - -// ErrCanceled is returned when the context cancels or times out -// an operation. -var ErrCanceled = errors.New("context is done") - -// readyCheckInterval is the interval to wait between checking -// the status in StatusChan. -// Unexported because 1 second is sensible, but configurable to make -// tests run quicker. -var readyCheckInterval = 1 * time.Second - -// StatusChan gets a channel that periodically gets the box info -// and sends a message whenever the status changes. -func StatusChan(ctx context.Context, i Box) <-chan string { - log.Println("boxutil: DEPRECATED use github.com/machinebox/sdk-go/boxutil instead") - statusChan := make(chan string) - go func() { - var lastStatus string - for { - select { - case <-ctx.Done(): - return - default: - time.Sleep(readyCheckInterval) - status := "unavailable" - info, err := i.Info() - if err == nil { - status = info.Status - } - if status != lastStatus { - lastStatus = status - statusChan <- status - } - } - } - }() - return statusChan -} - -// WaitForReady blocks until the Box is ready. -func WaitForReady(ctx context.Context, i Box) error { - log.Println("boxutil: DEPRECATED use github.com/machinebox/sdk-go/boxutil instead") - ctx, cancel := context.WithCancel(ctx) - defer cancel() - statusChan := StatusChan(ctx, i) - for { - select { - case <-ctx.Done(): - return ErrCanceled - case status := <-statusChan: - if IsReady(status) { - return nil - } - } - } -} - -// IsReady gets whether the box info status is ready or not. -func IsReady(status string) bool { - log.Println("boxutil: DEPRECATED use github.com/machinebox/sdk-go/boxutil instead") - return status == "ready" -} diff --git a/x/boxutil/status_test.go b/x/boxutil/status_test.go deleted file mode 100644 index 4279dba..0000000 --- a/x/boxutil/status_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package boxutil - -import ( - "context" - "errors" - "sync" - "testing" - "time" - - "github.com/matryer/is" -) - -func init() { - // quicker for testing - readyCheckInterval = 100 * time.Millisecond -} - -func TestStatusChan(t *testing.T) { - is := is.New(t) - - i := &testBox{} - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() - - status := StatusChan(ctx, i) - is.Equal(<-status, "starting...") - i.setReady() - is.Equal(<-status, "ready") - i.setError() - is.Equal(<-status, "unavailable") - i.clearError() - is.Equal(<-status, "ready") - -} - -func TestWaitForReady(t *testing.T) { - is := is.New(t) - i := &testBox{} - ctx, cancel := context.WithCancel(context.Background()) - time.AfterFunc(300*time.Millisecond, cancel) - go func() { - i.setReady() - }() - err := WaitForReady(ctx, i) - is.NoErr(err) -} - -func TestWaitForReadyTimeout(t *testing.T) { - is := is.New(t) - i := &testBox{} - ctx, cancel := context.WithCancel(context.Background()) - time.AfterFunc(100*time.Millisecond, cancel) - go func() { - time.Sleep(200 * time.Millisecond) - i.setReady() - }() - err := WaitForReady(ctx, i) - is.Equal(err, ErrCanceled) -} - -type testBox struct { - lock sync.Mutex - ready bool - err error -} - -func (i *testBox) setReady() { - i.lock.Lock() - defer i.lock.Unlock() - i.ready = true -} - -func (i *testBox) setError() { - i.lock.Lock() - defer i.lock.Unlock() - i.err = errors.New("cannot reach server") -} - -func (i *testBox) clearError() { - i.lock.Lock() - defer i.lock.Unlock() - i.err = nil -} - -func (i *testBox) Info() (*Info, error) { - i.lock.Lock() - defer i.lock.Unlock() - if i.err != nil { - return nil, i.err - } - if i.ready { - return &Info{Status: "ready"}, nil - } - return &Info{Status: "starting..."}, nil -}