golang.org/x/build@v0.0.0-20240506185731-218518f32b70/internal/coordinator/remote/remote_test.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build linux || darwin
     6  
     7  package remote
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"testing"
    13  	"time"
    14  
    15  	"golang.org/x/build/buildlet"
    16  )
    17  
    18  func TestSessionRenew(t *testing.T) {
    19  	start := time.Now()
    20  	s := Session{
    21  		Expires: start,
    22  	}
    23  	s.renew()
    24  	if !s.Expires.After(start) {
    25  		t.Errorf("Session.expires = %s; want a time > %s", s.Expires, start)
    26  	}
    27  }
    28  
    29  func TestSessionIsExpired(t *testing.T) {
    30  	testCases := []struct {
    31  		desc    string
    32  		expires time.Time
    33  		want    bool
    34  	}{
    35  		{"expire is zero value", time.Time{}, false},
    36  		{"expired", time.Now().Add(-time.Minute), true},
    37  		{"not expired", time.Now().Add(time.Minute), false},
    38  	}
    39  	for _, tc := range testCases {
    40  		t.Run(tc.desc, func(t *testing.T) {
    41  			s := &Session{
    42  				Expires: tc.expires,
    43  			}
    44  			if got := s.isExpired(); got != tc.want {
    45  				t.Errorf("Session.isExpired() = %t; want %t", got, tc.want)
    46  			}
    47  		})
    48  	}
    49  }
    50  
    51  func TestSessionPool(t *testing.T) {
    52  	sp := NewSessionPool(context.Background())
    53  	defer sp.Close()
    54  
    55  	wantInstances := 4
    56  	for i := 0; i < wantInstances; i++ {
    57  		sp.AddSession("accounts.google.com:user-xyz-124", "test-user", "builder-type-x", "host-type-x", &buildlet.FakeClient{})
    58  	}
    59  	sp.destroyExpiredSessions(context.Background())
    60  	if sp.Len() != wantInstances {
    61  		t.Errorf("SessionPool.Len() = %d; want %d", sp.Len(), wantInstances)
    62  	}
    63  }
    64  
    65  func TestSessionPoolList(t *testing.T) {
    66  	sp := NewSessionPool(context.Background())
    67  	defer sp.Close()
    68  
    69  	wantCount := 4
    70  	for i := 0; i < wantCount; i++ {
    71  		sp.AddSession("accounts.google.com:user-xyz-124", fmt.Sprintf("user-%d", i), "builder", "host", &buildlet.FakeClient{})
    72  	}
    73  	got := sp.List()
    74  	if len(got) != wantCount {
    75  		t.Errorf("SessionPool.List() = %v; want %d sessions", got, wantCount)
    76  	}
    77  	for it, s := range got[:len(got)-1] {
    78  		if s.ID > got[it+1].ID {
    79  			t.Fatalf("SessionPool.List(): SessionInstance[%d].ID=%s > SessionInstance[%d].ID=%s; want sorted by name",
    80  				it, s.ID, it+1, got[it+1].ID)
    81  		}
    82  	}
    83  }
    84  
    85  func TestSessionPoolDestroySession(t *testing.T) {
    86  	sp := NewSessionPool(context.Background())
    87  	defer sp.Close()
    88  
    89  	var sn []string
    90  	for i := 0; i < 4; i++ {
    91  		name := sp.AddSession("accounts.google.com:user-xyz-124", fmt.Sprintf("user-%d", i), "builder", "host", &buildlet.FakeClient{})
    92  		sn = append(sn, name)
    93  	}
    94  	for _, name := range sn {
    95  		if err := sp.DestroySession(name); err != nil {
    96  			t.Errorf("SessionPool.DestroySession(%q) = %s; want no error", name, err)
    97  		}
    98  	}
    99  }
   100  
   101  func TestRenewTimeout(t *testing.T) {
   102  	sp := NewSessionPool(context.Background())
   103  	defer sp.Close()
   104  
   105  	name := sp.AddSession("accounts.google.com:user-xyz-124", "user-x", "builder", "host", &buildlet.FakeClient{})
   106  	if err := sp.RenewTimeout(name); err != nil {
   107  		t.Errorf("SessionPool.RenewTimeout(%q) = %s; want no error", name, err)
   108  	}
   109  }
   110  
   111  func TestRenewTimeoutError(t *testing.T) {
   112  	sp := NewSessionPool(context.Background())
   113  	defer sp.Close()
   114  
   115  	name := sp.AddSession("accounts.google.com:user-xyz-124", "user-x", "builder", "host", &buildlet.FakeClient{})
   116  	if err := sp.RenewTimeout(name + "-wrong"); err == nil {
   117  		t.Errorf("SessionPool.RenewTimeout(%q) = %s; want error", name, err)
   118  	}
   119  }