...

Source file src/github.com/containerd/ttrpc/client_test.go

Documentation: github.com/containerd/ttrpc

     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 ttrpc
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/containerd/ttrpc/internal"
    25  )
    26  
    27  func TestUserOnCloseWait(t *testing.T) {
    28  	var (
    29  		ctx, cancel    = context.WithDeadline(context.Background(), time.Now().Add(1*time.Minute))
    30  		server         = mustServer(t)(NewServer())
    31  		testImpl       = &testingServer{}
    32  		addr, listener = newTestListener(t)
    33  	)
    34  
    35  	defer cancel()
    36  	defer listener.Close()
    37  
    38  	registerTestingService(server, testImpl)
    39  
    40  	go server.Serve(ctx, listener)
    41  	defer server.Shutdown(ctx)
    42  
    43  	var (
    44  		dataCh          = make(chan string)
    45  		client, cleanup = newTestClient(t, addr,
    46  			WithOnClose(func() {
    47  				dataCh <- time.Now().String()
    48  			}),
    49  		)
    50  
    51  		tp      internal.TestPayload
    52  		tclient = newTestingClient(client)
    53  	)
    54  
    55  	if _, err := tclient.Test(ctx, &tp); err != nil {
    56  		t.Fatal(err)
    57  	}
    58  
    59  	cleanup()
    60  
    61  	fctx, fcancel := context.WithDeadline(ctx, time.Now().Add(1*time.Second))
    62  	defer fcancel()
    63  	if err := client.UserOnCloseWait(fctx); err == nil || err != context.DeadlineExceeded {
    64  		t.Fatalf("expected error %v, but got %v", context.DeadlineExceeded, err)
    65  	}
    66  
    67  	<-dataCh
    68  
    69  	if err := client.UserOnCloseWait(ctx); err != nil {
    70  		t.Fatalf("expected error nil , but got %v", err)
    71  	}
    72  }
    73  

View as plain text