github.com/milvus-io/milvus-sdk-go/v2@v2.4.1/test/testcases/connect_test.go (about)

     1  //go:build L0
     2  
     3  package testcases
     4  
     5  import (
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/milvus-io/milvus-sdk-go/v2/client"
    10  
    11  	"github.com/milvus-io/milvus-sdk-go/v2/test/base"
    12  	"github.com/milvus-io/milvus-sdk-go/v2/test/common"
    13  )
    14  
    15  // test connect and close, connect again
    16  func TestConnectClose(t *testing.T) {
    17  	// connect
    18  	ctx := createContext(t, time.Second*common.DefaultTimeout)
    19  	mc, errConnect := base.NewMilvusClient(ctx, client.Config{Address: *addr})
    20  	common.CheckErr(t, errConnect, true)
    21  
    22  	// connect success
    23  	_, errList := mc.ListCollections(ctx)
    24  	common.CheckErr(t, errList, true)
    25  
    26  	// close success
    27  	mc.Close()
    28  	_, errList2 := mc.ListCollections(ctx)
    29  	common.CheckErr(t, errList2, false, "the client connection is closing")
    30  
    31  	// connect again
    32  	mc, errConnect2 := base.NewMilvusClient(ctx, client.Config{Address: *addr})
    33  	common.CheckErr(t, errConnect2, true)
    34  	_, errList3 := mc.ListCollections(ctx)
    35  	common.CheckErr(t, errList3, true)
    36  }
    37  
    38  func TestConnectCloseDefault(t *testing.T) {
    39  	// connect
    40  	ctx := createContext(t, time.Second*common.DefaultTimeout)
    41  	mc, errConnect := base.NewDefaultMilvusClient(ctx, *addr)
    42  	common.CheckErr(t, errConnect, true)
    43  
    44  	// connect success
    45  	_, errList := mc.ListCollections(ctx)
    46  	common.CheckErr(t, errList, true)
    47  
    48  	// close success
    49  	mc.Close()
    50  	_, errList2 := mc.ListCollections(ctx)
    51  	common.CheckErr(t, errList2, false, "the client connection is closing")
    52  
    53  	// connect again
    54  	mc, errConnect2 := base.NewDefaultMilvusClient(ctx, *addr)
    55  	common.CheckErr(t, errConnect2, true)
    56  	_, errList3 := mc.ListCollections(ctx)
    57  	common.CheckErr(t, errList3, true)
    58  }
    59  
    60  // test connect with timeout and invalid addr
    61  func TestConnectInvalidAddr(t *testing.T) {
    62  	// connect
    63  	ctx := createContext(t, time.Second*20)
    64  
    65  	_, errConnect := base.NewMilvusClient(ctx, client.Config{Address: "aa"})
    66  	common.CheckErr(t, errConnect, false, "context deadline exceeded")
    67  }
    68  
    69  // test connect repeatedly
    70  func TestConnectRepeat(t *testing.T) {
    71  	// connect
    72  	ctx := createContext(t, time.Second*10)
    73  
    74  	_, errConnect := base.NewMilvusClient(ctx, client.Config{Address: *addr})
    75  	common.CheckErr(t, errConnect, true)
    76  
    77  	// connect again
    78  	mc, errConnect2 := base.NewMilvusClient(ctx, client.Config{Address: *addr})
    79  	common.CheckErr(t, errConnect2, true)
    80  
    81  	_, err := mc.ListCollections(ctx)
    82  	common.CheckErr(t, err, true)
    83  }
    84  
    85  // test close repeatedly
    86  func TestCloseRepeat(t *testing.T) {
    87  	// connect
    88  	ctx := createContext(t, time.Second*10)
    89  	mc, errConnect2 := base.NewMilvusClient(ctx, client.Config{Address: *addr})
    90  	common.CheckErr(t, errConnect2, true)
    91  
    92  	// close and again
    93  	mc.Close()
    94  	mc.Close()
    95  }