github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/client_ttrpc_test.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package containerd
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	v1 "github.com/containerd/containerd/api/services/ttrpc/events/v1"
    25  	"github.com/containerd/containerd/namespaces"
    26  	"github.com/containerd/containerd/pkg/ttrpcutil"
    27  	"github.com/containerd/ttrpc"
    28  	"github.com/gogo/protobuf/types"
    29  	"gotest.tools/v3/assert"
    30  )
    31  
    32  func TestClientTTRPC_New(t *testing.T) {
    33  	client, err := ttrpcutil.NewClient(address + ".ttrpc")
    34  	assert.NilError(t, err)
    35  
    36  	err = client.Close()
    37  	assert.NilError(t, err)
    38  }
    39  
    40  func TestClientTTRPC_Reconnect(t *testing.T) {
    41  	client, err := ttrpcutil.NewClient(address + ".ttrpc")
    42  	assert.NilError(t, err)
    43  
    44  	err = client.Reconnect()
    45  	assert.NilError(t, err)
    46  
    47  	service, err := client.EventsService()
    48  	assert.NilError(t, err)
    49  
    50  	// Send test request to make sure its alive after reconnect
    51  	_, err = service.Forward(context.Background(), &v1.ForwardRequest{
    52  		Envelope: &v1.Envelope{
    53  			Timestamp: time.Now(),
    54  			Namespace: namespaces.Default,
    55  			Topic:     "/test",
    56  			Event:     &types.Any{},
    57  		},
    58  	})
    59  	assert.NilError(t, err)
    60  
    61  	err = client.Close()
    62  	assert.NilError(t, err)
    63  }
    64  
    65  func TestClientTTRPC_Close(t *testing.T) {
    66  	client, err := ttrpcutil.NewClient(address + ".ttrpc")
    67  	assert.NilError(t, err)
    68  
    69  	service, err := client.EventsService()
    70  	assert.NilError(t, err)
    71  
    72  	err = client.Close()
    73  	assert.NilError(t, err)
    74  
    75  	_, err = service.Forward(context.Background(), &v1.ForwardRequest{Envelope: &v1.Envelope{}})
    76  	assert.Equal(t, err, ttrpc.ErrClosed)
    77  
    78  	err = client.Close()
    79  	assert.NilError(t, err)
    80  }