github.com/vmware/govmomi@v0.51.0/session/keep_alive_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package session_test 6 7 import ( 8 "context" 9 "errors" 10 "sync" 11 "testing" 12 "time" 13 14 "github.com/vmware/govmomi/session" 15 "github.com/vmware/govmomi/simulator" 16 "github.com/vmware/govmomi/vim25" 17 "github.com/vmware/govmomi/vim25/soap" 18 ) 19 20 func TestKeepAlive(t *testing.T) { 21 simulator.Test(func(ctx context.Context, c *vim25.Client) { 22 m := session.NewManager(c) 23 24 err := m.Logout(ctx) 25 if err != nil { 26 t.Fatal(err) 27 } 28 29 var mu sync.Mutex 30 n := 0 31 c.RoundTripper = vim25.Retry(c.Client, vim25.RetryTemporaryNetworkError, 3) 32 c.RoundTripper = session.KeepAliveHandler(c.RoundTripper, time.Millisecond, func(soap.RoundTripper) error { 33 mu.Lock() 34 n++ 35 mu.Unlock() 36 return errors.New("stop") // stops the keep alive routine 37 }) 38 39 err = m.Login(ctx, simulator.DefaultLogin) // starts the keep alive routine 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 time.Sleep(time.Millisecond * 10) 45 mu.Lock() 46 if n != 1 { 47 t.Errorf("handler called %d times", n) 48 } 49 mu.Unlock() 50 }) 51 }