github.com/mailru/activerecord@v1.12.2/pkg/octopus/connection_w_test.go (about) 1 package octopus 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 ) 8 9 func Test_prepareConnection(t *testing.T) { 10 type args struct { 11 server string 12 opts []ConnectionOption 13 } 14 tests := []struct { 15 name string 16 args args 17 want string 18 wantErr bool 19 }{ 20 { 21 name: "connectionHash", 22 args: args{ 23 server: "127.0.0.1", 24 opts: []ConnectionOption{}, 25 }, 26 want: "ff9ed3cc", 27 wantErr: false, 28 }, 29 { 30 name: "same connectionHash", 31 args: args{ 32 server: "127.0.0.1", 33 opts: []ConnectionOption{}, 34 }, 35 want: "ff9ed3cc", 36 wantErr: false, 37 }, 38 { 39 name: "connectionHash with options", 40 args: args{ 41 server: "127.0.0.1", 42 opts: []ConnectionOption{ 43 WithTimeout(time.Millisecond*50, time.Millisecond*100), 44 }, 45 }, 46 want: "f855b29a", 47 wantErr: false, 48 }, 49 { 50 name: "yes another connectionHash with options", 51 args: args{ 52 server: "127.0.0.1", 53 opts: []ConnectionOption{ 54 WithTimeout(time.Millisecond*50, time.Millisecond*100), 55 WithIntervals(time.Second*50, time.Second*50, time.Second*50), 56 }, 57 }, 58 want: "fdef5d9b", 59 wantErr: false, 60 }, 61 { 62 name: "yes another connectionHash with options", 63 args: args{ 64 server: "", 65 opts: []ConnectionOption{ 66 WithTimeout(time.Millisecond*50, time.Millisecond*100), 67 WithIntervals(time.Second*50, time.Second*50, time.Second*50), 68 }, 69 }, 70 want: "", 71 wantErr: true, 72 }, 73 } 74 for _, tt := range tests { 75 t.Run(tt.name, func(t *testing.T) { 76 got, err := NewOptions(tt.args.server, ModeMaster, tt.args.opts...) 77 if (err != nil) != tt.wantErr { 78 t.Errorf("prepareConnection() error = %v, wantErr %v", err, tt.wantErr) 79 return 80 } 81 if !tt.wantErr && got.GetConnectionID() != tt.want { 82 t.Errorf("prepareConnection() Hex = %v, want %v", got.GetConnectionID(), tt.want) 83 } 84 }) 85 } 86 } 87 88 func TestGetConnection(t *testing.T) { 89 type args struct { 90 ctx context.Context 91 server string 92 opts []ConnectionOption 93 } 94 tests := []struct { 95 name string 96 args args 97 want []string 98 wantErr bool 99 }{ 100 { 101 name: "first connection", 102 args: args{ 103 ctx: context.Background(), 104 server: "127.0.0.1:11211", 105 opts: []ConnectionOption{}, 106 }, 107 wantErr: false, 108 want: []string{""}, 109 }, 110 } 111 112 oms, err := InitMockServer(WithHost("127.0.0.1", "11211")) 113 if err != nil { 114 t.Fatalf("error init octopusMock %s", err) 115 return 116 } 117 118 err = oms.Start() 119 if err != nil { 120 t.Fatalf("error start octopusMock %s", err) 121 return 122 } 123 124 defer func() { 125 err := oms.Stop() 126 if err != nil { 127 t.Fatalf("error stop octopusMock %s", err) 128 } 129 }() 130 131 for _, tt := range tests { 132 t.Run(tt.name, func(t *testing.T) { 133 octopusOpts, err := NewOptions(tt.args.server, ModeMaster, tt.args.opts...) 134 if err != nil { 135 t.Errorf("can't initialize options: %s", err) 136 } 137 _, err = GetConnection(tt.args.ctx, octopusOpts) 138 if (err != nil) != tt.wantErr { 139 t.Errorf("GetConnection() error = %v, wantErr %v", err, tt.wantErr) 140 return 141 } 142 }) 143 } 144 }