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