github.com/vmware/govmomi@v0.43.0/session/keepalive/example_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 "fmt" 22 "log" 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/vim25" 30 ) 31 32 var ( 33 sessionCheckPause = time.Second / 2 34 sessionIdleTimeout = sessionCheckPause / 2 35 keepAliveIdle = sessionIdleTimeout / 2 36 ) 37 38 func init() { 39 simulator.SessionIdleTimeout = sessionIdleTimeout 40 } 41 42 func ExampleHandlerSOAP() { 43 simulator.Run(func(ctx context.Context, c *vim25.Client) error { 44 // No need for initial Login() here as simulator.Run already has 45 m := session.NewManager(c) 46 47 // check twice if session is valid, sleeping > SessionIdleTimeout in between 48 check := func() { 49 for i := 0; i < 2; i++ { 50 s, err := m.UserSession(ctx) 51 if err != nil { 52 log.Fatal(err) 53 } 54 55 fmt.Printf("session valid=%t\n", s != nil) 56 if i == 0 { 57 time.Sleep(sessionCheckPause) 58 } 59 } 60 } 61 62 // session will expire here 63 check() 64 65 // this starts the keep alive handler when Login is called, and stops the handler when Logout is called 66 c.RoundTripper = keepalive.NewHandlerSOAP(c.RoundTripper, keepAliveIdle, nil) 67 68 err := m.Login(ctx, simulator.DefaultLogin) 69 if err != nil { 70 return err 71 } 72 73 // session will not expire here, with the keep alive handler in place. 74 check() 75 76 err = m.Logout(ctx) 77 if err != nil { 78 return err 79 } 80 81 // Logout() also stops the keep alive handler, session is no longer valid. 82 check() 83 84 return nil 85 }) 86 // Output: 87 // session valid=true 88 // session valid=false 89 // session valid=true 90 // session valid=true 91 // session valid=false 92 // session valid=false 93 } 94 95 func ExampleHandlerREST() { 96 simulator.Run(func(ctx context.Context, vc *vim25.Client) error { 97 c := rest.NewClient(vc) 98 err := c.Login(ctx, simulator.DefaultLogin) 99 if err != nil { 100 return err 101 } 102 103 // check twice if session is valid, sleeping > SessionIdleTimeout in between. 104 check := func() { 105 for i := 0; i < 2; i++ { 106 s, err := c.Session(ctx) 107 if err != nil { 108 log.Fatal(err) 109 } 110 111 fmt.Printf("session valid=%t\n", s != nil) 112 if i == 0 { 113 time.Sleep(sessionCheckPause) 114 } 115 } 116 } 117 118 // session will expire here 119 check() 120 121 // this starts the keep alive handler when Login is called, and stops the handler when Logout is called 122 c.Transport = keepalive.NewHandlerREST(c, keepAliveIdle, nil) 123 124 err = c.Login(ctx, simulator.DefaultLogin) 125 if err != nil { 126 return err 127 } 128 129 // session will not expire here, with the keep alive handler in place. 130 check() 131 132 err = c.Logout(ctx) 133 if err != nil { 134 return err 135 } 136 137 // Logout() also stops the keep alive handler, session is no longer valid. 138 check() 139 140 return nil 141 }) 142 // Output: 143 // session valid=true 144 // session valid=false 145 // session valid=true 146 // session valid=true 147 // session valid=false 148 // session valid=false 149 }