github.com/vmware/govmomi@v0.43.0/session/keepalive/handler_test.go (about) 1 /* 2 Copyright (c) 2020 VMware, Inc. All Rights Reserved. 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 keepalive_test 18 19 import ( 20 "context" 21 "sync" 22 "testing" 23 "time" 24 25 "github.com/vmware/govmomi/session" 26 "github.com/vmware/govmomi/session/keepalive" 27 "github.com/vmware/govmomi/simulator" 28 "github.com/vmware/govmomi/vapi/rest" 29 _ "github.com/vmware/govmomi/vapi/simulator" 30 "github.com/vmware/govmomi/vim25" 31 "github.com/vmware/govmomi/vim25/soap" 32 ) 33 34 type count struct { 35 sync.Mutex 36 val int32 37 } 38 39 func (t *count) Send() error { 40 t.Lock() 41 defer t.Unlock() 42 t.val++ 43 return nil 44 } 45 46 func (t *count) Value() int { 47 t.Lock() 48 defer t.Unlock() 49 return int(t.val) 50 } 51 52 func TestHandlerSOAP(t *testing.T) { 53 simulator.Test(func(ctx context.Context, c *vim25.Client) { 54 var i count 55 56 sc := soap.NewClient(c.URL(), true) 57 vc, err := vim25.NewClient(ctx, sc) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 vc.RoundTripper = keepalive.NewHandlerSOAP(sc, time.Millisecond, i.Send) 63 64 m := session.NewManager(vc) 65 66 // Expect keep alive to not have triggered yet 67 v := i.Value() 68 if v != 0 { 69 t.Errorf("Expected i == 0, got i: %d", v) 70 } 71 72 // Logging in starts keep alive 73 err = m.Login(ctx, simulator.DefaultLogin) 74 if err != nil { 75 t.Error(err) 76 } 77 78 time.Sleep(10 * time.Millisecond) 79 80 // Expect keep alive to triggered at least once 81 v = i.Value() 82 if v == 0 { 83 t.Errorf("Expected i != 0, got i: %d", v) 84 } 85 86 j := i.Value() 87 time.Sleep(10 * time.Millisecond) 88 89 // Expect keep alive to triggered at least once more 90 v = i.Value() 91 if v <= j { 92 t.Errorf("Expected i > j, got i: %d, j: %d", v, j) 93 } 94 95 // Logging out stops keep alive 96 err = m.Logout(ctx) 97 if err != nil { 98 t.Error(err) 99 } 100 101 j = i.Value() 102 time.Sleep(10 * time.Millisecond) 103 104 // Expect keep alive to have stopped 105 v = i.Value() 106 if v != j { 107 t.Errorf("Expected i == j, got i: %d, j: %d", v, j) 108 } 109 }) 110 } 111 112 func TestHandlerREST(t *testing.T) { 113 simulator.Test(func(ctx context.Context, c *vim25.Client) { 114 var i count 115 116 sc := soap.NewClient(c.URL(), true) 117 vc, err := vim25.NewClient(ctx, sc) 118 if err != nil { 119 t.Fatal(err) 120 } 121 122 rc := rest.NewClient(vc) 123 rc.Transport = keepalive.NewHandlerREST(rc, time.Millisecond, i.Send) 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 // Expect keep alive to not have triggered yet 129 v := i.Value() 130 if v != 0 { 131 t.Errorf("Expected i == 0, got i: %d", v) 132 } 133 134 // Logging in starts keep alive 135 err = rc.Login(ctx, simulator.DefaultLogin) 136 if err != nil { 137 t.Error(err) 138 } 139 140 time.Sleep(10 * time.Millisecond) 141 142 // Expect keep alive to triggered at least once 143 v = i.Value() 144 if v == 0 { 145 t.Errorf("Expected i != 0, got i: %d", v) 146 } 147 148 j := i.Value() 149 time.Sleep(10 * time.Millisecond) 150 151 // Expect keep alive to triggered at least once more 152 v = i.Value() 153 if v <= j { 154 t.Errorf("Expected i > j, got i: %d, j: %d", v, j) 155 } 156 157 // Logging out stops keep alive 158 err = rc.Logout(ctx) 159 if err != nil { 160 t.Error(err) 161 } 162 163 j = i.Value() 164 time.Sleep(10 * time.Millisecond) 165 166 // Expect keep alive to have stopped 167 v = i.Value() 168 if v != j { 169 t.Errorf("Expected i == j, got i: %d, j: %d", v, j) 170 } 171 }) 172 }