github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/server/internal/cache/cache_test.go (about)

     1  // Copyright 2018 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cache
    16  
    17  import (
    18  	"bytes"
    19  	"sync/atomic"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/google/fleetspeak/fleetspeak/src/common"
    24  	"github.com/google/fleetspeak/fleetspeak/src/server/db"
    25  	"github.com/google/fleetspeak/fleetspeak/src/server/internal/ftime"
    26  )
    27  
    28  func TestClients(t *testing.T) {
    29  	oi := expireInterval
    30  	expireInterval = time.Second
    31  	defer func() {
    32  		expireInterval = oi
    33  	}()
    34  
    35  	// We'd use sertesting.FakeNow, but that creates a dependency loop. So we
    36  	// do it directly.
    37  	otime := ftime.Now
    38  	fakeTime := int64(20000)
    39  	ftime.Now = func() time.Time {
    40  		return time.Unix(atomic.LoadInt64(&fakeTime), 0).UTC()
    41  	}
    42  	defer func() {
    43  		ftime.Now = otime
    44  	}()
    45  
    46  	c := NewClients()
    47  	defer c.Stop()
    48  
    49  	id1, _ := common.StringToClientID("0000000000000001")
    50  	id2, _ := common.StringToClientID("0000000000000002")
    51  
    52  	for _, id := range []common.ClientID{id1, id2} {
    53  		got := c.Get(id)
    54  		if got != nil {
    55  			t.Errorf("Get(%v) = %v, expected nil", id, got)
    56  		}
    57  	}
    58  
    59  	c.Update(id1, &db.ClientData{
    60  		Key: []byte("key 1"),
    61  	})
    62  
    63  	atomic.StoreInt64(&fakeTime, 20044)
    64  
    65  	got := c.Get(id1)
    66  	if got == nil || !bytes.Equal(got.Key, []byte("key 1")) {
    67  		t.Errorf("Get(%v) = %v, expected {Key: \"key 1\"}", id1, got)
    68  	}
    69  	got = c.Get(id2)
    70  	if got != nil {
    71  		t.Errorf("Get(%v) = %v, expected nil", id2, got)
    72  	}
    73  
    74  	// Set key 2, make sure that clearing it works.
    75  	c.Update(id2, &db.ClientData{
    76  		Key: []byte("key 2"),
    77  	})
    78  	s := c.Size()
    79  	if s != 2 {
    80  		t.Errorf("Expected cache size of 2, got Size: %d", s)
    81  	}
    82  	c.Update(id2, nil)
    83  	got = c.Get(id2)
    84  	if got != nil {
    85  		t.Errorf("Get(%v) = %v, expected nil", id2, got)
    86  	}
    87  	// A second clear should not panic.
    88  	c.Update(id2, nil)
    89  
    90  	// Advance the clock enough to expire id1, wait for expire to run.
    91  	atomic.StoreInt64(&fakeTime, 20046)
    92  	time.Sleep(2 * time.Second)
    93  
    94  	// Everything should be nil.
    95  	for _, id := range []common.ClientID{id1, id2} {
    96  		got := c.Get(id)
    97  		if got != nil {
    98  			t.Errorf("Get(%v) = %v, expected nil", id, got)
    99  		}
   100  	}
   101  
   102  	// We shouldn't be using any extra ram.
   103  	s = c.Size()
   104  	if s != 0 {
   105  		t.Errorf("Expected empty cache, got Size: %d", s)
   106  	}
   107  }