github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/migrationminion/client_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationminion_test
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	jujutesting "github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	apitesting "github.com/juju/juju/api/base/testing"
    15  	"github.com/juju/juju/api/migrationminion"
    16  	"github.com/juju/juju/apiserver/params"
    17  	coretesting "github.com/juju/juju/testing"
    18  	"github.com/juju/juju/worker"
    19  )
    20  
    21  type ClientSuite struct {
    22  	jujutesting.IsolationSuite
    23  }
    24  
    25  var _ = gc.Suite(&ClientSuite{})
    26  
    27  func (s *ClientSuite) TestWatch(c *gc.C) {
    28  	var stub jujutesting.Stub
    29  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    30  		stub.AddCall(objType+"."+request, id, arg)
    31  		switch request {
    32  		case "Watch":
    33  			*(result.(*params.NotifyWatchResult)) = params.NotifyWatchResult{
    34  				NotifyWatcherId: "abc",
    35  			}
    36  		case "Next":
    37  			// The full success case is tested in api/watcher.
    38  			return errors.New("boom")
    39  		case "Stop":
    40  		}
    41  		return nil
    42  	})
    43  
    44  	client := migrationminion.NewClient(apiCaller)
    45  	w, err := client.Watch()
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	defer worker.Stop(w)
    48  
    49  	errC := make(chan error)
    50  	go func() {
    51  		errC <- w.Wait()
    52  	}()
    53  
    54  	select {
    55  	case err := <-errC:
    56  		c.Assert(err, gc.ErrorMatches, "boom")
    57  		expectedCalls := []jujutesting.StubCall{
    58  			{"Migrationminion.Watch", []interface{}{"", nil}},
    59  			{"MigrationStatusWatcher.Next", []interface{}{"abc", nil}},
    60  			{"MigrationStatusWatcher.Stop", []interface{}{"abc", nil}},
    61  		}
    62  		// The Stop API call happens in a separate goroutine which
    63  		// might execute after the worker has exited so wait for the
    64  		// expected calls to arrive.
    65  		for a := coretesting.LongAttempt.Start(); a.Next(); {
    66  			if len(stub.Calls()) >= len(expectedCalls) {
    67  				return
    68  			}
    69  		}
    70  		stub.CheckCalls(c, expectedCalls)
    71  	case <-time.After(coretesting.LongWait):
    72  		c.Fatal("timed out waiting for watcher to die")
    73  	}
    74  }
    75  
    76  func (s *ClientSuite) TestWatchErr(c *gc.C) {
    77  	apiCaller := apitesting.APICallerFunc(func(objType string, version int, id, request string, arg, result interface{}) error {
    78  		return errors.New("boom")
    79  	})
    80  	client := migrationminion.NewClient(apiCaller)
    81  	_, err := client.Watch()
    82  	c.Assert(err, gc.ErrorMatches, "boom")
    83  }