diff options
Diffstat (limited to 'test/sdks_test.go')
-rw-r--r-- | test/sdks_test.go | 83 |
1 files changed, 13 insertions, 70 deletions
diff --git a/test/sdks_test.go b/test/sdks_test.go index 51f1d95..9c3fc7e 100644 --- a/test/sdks_test.go +++ b/test/sdks_test.go @@ -17,82 +17,25 @@ package xdsservertest import ( - "fmt" - "log" "os" "path" "regexp" - "sync" "testing" "time" "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" - socketio_client "github.com/sebd71/go-socket.io-client" "github.com/stretchr/testify/assert" ) -var ch chan xsapiv1.SDK - -// Disconnection Channel used to notify XDS Server disconnection -//type Disconnection struct { -// error string -// code int -// svrConf ServerCfg -//} - -// IOSockClient . -type IOSockClient struct { - URL string - Conn *socketio_client.Client - Options *socketio_client.Options - EmitMutex *sync.Mutex - Connected bool - //ServerDiscoChan chan Disconnection - EscapeKeys []byte -} - -func NewIoSocketClient(url, clientID string) (*IOSockClient, error) { - - var err error - - sCli := &IOSockClient{ - URL: url, - EmitMutex: &sync.Mutex{}, - Options: &socketio_client.Options{ - Transport: "websocket", - Header: make(map[string][]string), - }, - //ServerDiscoChan: make(chan Disconnection, 1), - } - sCli.Options.Header["XDS-SID"] = []string{clientID} - - sCli.Conn, err = socketio_client.NewClient(url, sCli.Options) - if err != nil { - return nil, fmt.Errorf("IO.socket connection error: " + err.Error()) - } - - sCli.Conn.On("connection", func() { - sCli.Connected = true - }) - - sCli.Conn.On("disconnection", func(err error) { - log.Printf("WS disconnection event with err: %v\n", err) - sCli.Connected = false - }) +func ConnectSDKStateChange(t *testing.T, sCli *IOSockClient, channel chan xsapiv1.SDK) { sCli.Conn.On(xsapiv1.EVTSDKStateChange, func(e xsapiv1.EventMsg) { sdk, _ := e.DecodeSDKEvent() - ch <- sdk + channel <- sdk }) args := xsapiv1.EventRegisterArgs{Name: xsapiv1.EVTSDKStateChange} - if err := HTTPCli.Post("/events/register", args, nil); err != nil { - return sCli, err - } - - //sCli.Conn.On(xsapiv1.) - - return sCli, nil + assert.Nil(t, HTTPCli.Post("/events/register", args, nil)) } func TestSdks(t *testing.T) { @@ -101,12 +44,9 @@ func TestSdks(t *testing.T) { _, err := os.Stat(path.Join(sdkDir, "sdks_latest.json")) assert.Nil(t, err) t.Logf("Connect websocket with url=%v clientId=%v\n", prefixURL, HTTPCli.GetClientID()) - _, err = NewIoSocketClient(prefixURL, HTTPCli.GetClientID()) - if err != nil { - t.Fatal(err) - } - ch = make(chan xsapiv1.SDK) + chSdks := make(chan xsapiv1.SDK) + ConnectSDKStateChange(t, sCli, chSdks) for i := 0; i < len(sdkArray); i++ { re := regexp.MustCompile("^[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+-[0-9a-z]+$") @@ -124,18 +64,18 @@ func TestSdks(t *testing.T) { assert.Nil(t, HTTPCli.Post("/sdks/abortinstall", sdk, &sdkRes)) assert.Equal(t, sdkRes.Status, "Not Installed") time.Sleep(1 * time.Millisecond) - sdkFromEvent := <-ch + sdkFromEvent := <-chSdks assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) assert.Equal(t, sdkRes.Status, sdkFromEvent.Status) } } -func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs) xsapiv1.SDK { +func installFakeSdk(t *testing.T, sdkInstall xsapiv1.SDKInstallArgs, chSdks chan xsapiv1.SDK) xsapiv1.SDK { var sdkRes xsapiv1.SDK assert.Nil(t, HTTPCli.Post("/sdks", sdkInstall, &sdkRes)) assert.Equal(t, sdkRes.Status, "Installing") /*waiting for SDKStateChange event from channel*/ - sdkFromEvent := <-ch + sdkFromEvent := <-chSdks t.Log(sdkFromEvent) assert.Equal(t, sdkRes.ID, sdkFromEvent.ID) assert.Equal(t, "Installed", sdkFromEvent.Status) @@ -148,11 +88,14 @@ func TestInstallFakeSdk(t *testing.T) { Filename: sdkFileName, Force: false, } - installFakeSdk(t, sdk) + + chSdks := make(chan xsapiv1.SDK) + ConnectSDKStateChange(t, sCli, chSdks) + installFakeSdk(t, sdk, chSdks) var sdkRes xsapiv1.SDK assert.NotNil(t, HTTPCli.Post("/sdks", sdk, &sdkRes)) sdk.Force = true - //installFakeSdk(t, sdk) + //installFakeSdk(t, sdk, chSdks) } func TestSdksError(t *testing.T) { |