github.com/HashDataInc/packer@v1.3.2/packer/rpc/cache_test.go (about)

     1  package rpc
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/packer/packer"
     7  )
     8  
     9  type testCache struct {
    10  	lockCalled    bool
    11  	lockKey       string
    12  	unlockCalled  bool
    13  	unlockKey     string
    14  	rlockCalled   bool
    15  	rlockKey      string
    16  	runlockCalled bool
    17  	runlockKey    string
    18  }
    19  
    20  func (t *testCache) Lock(key string) string {
    21  	t.lockCalled = true
    22  	t.lockKey = key
    23  	return "foo"
    24  }
    25  
    26  func (t *testCache) RLock(key string) (string, bool) {
    27  	t.rlockCalled = true
    28  	t.rlockKey = key
    29  	return "foo", true
    30  }
    31  
    32  func (t *testCache) Unlock(key string) {
    33  	t.unlockCalled = true
    34  	t.unlockKey = key
    35  }
    36  
    37  func (t *testCache) RUnlock(key string) {
    38  	t.runlockCalled = true
    39  	t.runlockKey = key
    40  }
    41  
    42  func TestCache_Implements(t *testing.T) {
    43  	var _ packer.Cache = new(cache)
    44  }
    45  
    46  func TestCacheRPC(t *testing.T) {
    47  	// Create the interface to test
    48  	c := new(testCache)
    49  
    50  	// Start the server
    51  	client, server := testClientServer(t)
    52  	defer client.Close()
    53  	defer server.Close()
    54  	server.RegisterCache(c)
    55  
    56  	cacheClient := client.Cache()
    57  
    58  	// Test Lock
    59  	cacheClient.Lock("foo")
    60  	if !c.lockCalled {
    61  		t.Fatal("should be called")
    62  	}
    63  	if c.lockKey != "foo" {
    64  		t.Fatalf("bad: %s", c.lockKey)
    65  	}
    66  
    67  	// Test Unlock
    68  	cacheClient.Unlock("foo")
    69  	if !c.unlockCalled {
    70  		t.Fatal("should be called")
    71  	}
    72  	if c.unlockKey != "foo" {
    73  		t.Fatalf("bad: %s", c.unlockKey)
    74  	}
    75  
    76  	// Test RLock
    77  	cacheClient.RLock("foo")
    78  	if !c.rlockCalled {
    79  		t.Fatal("should be called")
    80  	}
    81  	if c.rlockKey != "foo" {
    82  		t.Fatalf("bad: %s", c.rlockKey)
    83  	}
    84  
    85  	// Test RUnlock
    86  	cacheClient.RUnlock("foo")
    87  	if !c.runlockCalled {
    88  		t.Fatal("should be called")
    89  	}
    90  	if c.runlockKey != "foo" {
    91  		t.Fatalf("bad: %s", c.runlockKey)
    92  	}
    93  }