github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/cache_test.go (about)

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