Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package lib |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | |
| 7 | mocks_ipc "veyron/runtimes/google/testing/mocks/ipc" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 8 | "veyron2/ipc" |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 9 | "veyron2/rt" |
Todd Wang | 0ecdd7a | 2014-07-14 16:24:38 -0700 | [diff] [blame] | 10 | "veyron2/vdl/vdlutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 11 | "veyron2/wiretype" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | name = "/veyron/name" |
| 16 | ) |
| 17 | |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 18 | func init() { |
| 19 | rt.Init() |
| 20 | } |
| 21 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 22 | func expectedSignature() ipc.ServiceSignature { |
| 23 | return ipc.ServiceSignature{ |
| 24 | Methods: make(map[string]ipc.MethodSignature), |
Todd Wang | 0ecdd7a | 2014-07-14 16:24:38 -0700 | [diff] [blame] | 25 | TypeDefs: []vdlutil.Any{ |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 26 | wiretype.NamedPrimitiveType{ |
Todd Wang | 0ecdd7a | 2014-07-14 16:24:38 -0700 | [diff] [blame] | 27 | Name: "veyron2/vdlutil.AnyData", |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 28 | Type: wiretype.TypeIDInterface, |
| 29 | }, |
| 30 | }, |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func client() *mocks_ipc.SimpleMockClient { |
| 35 | return mocks_ipc.NewSimpleClient( |
| 36 | map[string][]interface{}{ |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 37 | "Signature": []interface{}{expectedSignature(), nil}, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 38 | }, |
| 39 | ) |
| 40 | } |
| 41 | |
| 42 | func assertMethodSignatureAsExpected(t *testing.T, got, expected ipc.MethodSignature) { |
| 43 | if !reflect.DeepEqual(got.InArgs, expected.InArgs) { |
| 44 | t.Errorf(`InArgs do not match: result "%v", want "%v"`, got.InArgs, expected.InArgs) |
| 45 | return |
| 46 | } |
| 47 | if !reflect.DeepEqual(got.OutArgs, expected.OutArgs) { |
| 48 | t.Errorf(`OutArgs do not match: result "%v", want "%v"`, got.OutArgs, expected.OutArgs) |
| 49 | return |
| 50 | } |
| 51 | if got.InStream != expected.InStream { |
| 52 | t.Errorf(`InStreams do not match: result "%v", want "%v"`, got.InStream, expected.InStream) |
| 53 | return |
| 54 | } |
| 55 | if got.OutStream != expected.OutStream { |
| 56 | t.Errorf(`OutStream do not match: result "%v", want "%v"`, got.OutStream, expected.OutStream) |
| 57 | return |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func assertSignatureAsExpected(t *testing.T, got, expected *ipc.ServiceSignature) { |
| 62 | if !reflect.DeepEqual(got.TypeDefs, expected.TypeDefs) { |
| 63 | t.Errorf(`TypeDefs do not match: result "%v", want "%v"`, got.TypeDefs, expected.TypeDefs) |
| 64 | return |
| 65 | } |
| 66 | if n, m := len(got.Methods), len(expected.Methods); n != m { |
| 67 | t.Errorf(`Wrong number of signature methods: result "%d", want "%d"`, n, m) |
| 68 | return |
| 69 | } |
| 70 | for gotName, gotMethod := range got.Methods { |
| 71 | expectedMethod, ok := expected.Methods[gotName] |
| 72 | if !ok { |
| 73 | t.Errorf(`Method "%v" was expected but not found`, gotName) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | assertMethodSignatureAsExpected(t, gotMethod, expectedMethod) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestFetching(t *testing.T) { |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 82 | sm := NewSignatureManager() |
| 83 | got, err := sm.Signature(rt.R().NewContext(), name, client()) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Errorf(`Did not expect an error but got %v`, err) |
| 86 | return |
| 87 | } |
| 88 | expected := expectedSignature() |
| 89 | assertSignatureAsExpected(t, got, &expected) |
| 90 | } |
| 91 | |
| 92 | func TestThatCachedAfterFetching(t *testing.T) { |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 93 | sm := NewSignatureManager().(*signatureManager) |
| 94 | sig, _ := sm.Signature(rt.R().NewContext(), name, client()) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 95 | cache, ok := sm.cache[name] |
| 96 | if !ok { |
| 97 | t.Errorf(`Signature manager did not cache the results`) |
| 98 | return |
| 99 | } |
| 100 | assertSignatureAsExpected(t, &cache.signature, sig) |
| 101 | } |
| 102 | |
| 103 | func TestThatCacheIsUsed(t *testing.T) { |
| 104 | client := client() |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 105 | sm := NewSignatureManager() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 106 | |
| 107 | // call twice |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 108 | sm.Signature(rt.R().NewContext(), name, client) |
| 109 | sm.Signature(rt.R().NewContext(), name, client) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 110 | |
| 111 | // expect number of calls to Signature method of client to still be 1 since cache |
| 112 | // should have been used despite the second call |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 113 | if client.TimesCalled("Signature") != 1 { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 114 | t.Errorf("Signature cache was not used for the second call") |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestThatLastAccessedGetUpdated(t *testing.T) { |
| 119 | client := client() |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 120 | sm := NewSignatureManager().(*signatureManager) |
| 121 | sm.Signature(rt.R().NewContext(), name, client) |
Ali Ghassemi | 598309d | 2014-05-27 08:57:53 -0700 | [diff] [blame] | 122 | // make last accessed be in the past to account for the fact that |
| 123 | // two consecutive calls to time.Now() can return identical values. |
| 124 | sm.cache[name].lastAccessed = sm.cache[name].lastAccessed.Add(-ttl / 2) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 125 | prevAccess := sm.cache[name].lastAccessed |
| 126 | |
| 127 | // access again |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 128 | sm.Signature(rt.R().NewContext(), name, client) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 129 | newAccess := sm.cache[name].lastAccessed |
| 130 | |
| 131 | if !newAccess.After(prevAccess) { |
| 132 | t.Errorf("LastAccessed was not updated for cache entry") |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestThatTTLExpires(t *testing.T) { |
| 137 | client := client() |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 138 | sm := NewSignatureManager().(*signatureManager) |
| 139 | sm.Signature(rt.R().NewContext(), name, client) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 140 | |
| 141 | // make last accessed go over the ttl |
Shyam Jayaraman | c0b0d8a | 2014-05-13 11:31:00 -0700 | [diff] [blame] | 142 | sm.cache[name].lastAccessed = sm.cache[name].lastAccessed.Add(-2 * ttl) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 143 | |
| 144 | // make a second call |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 145 | sm.Signature(rt.R().NewContext(), name, client) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 146 | |
| 147 | // expect number of calls to Signature method of client to be 2 since cache should have expired |
Shyam Jayaraman | 72718c9 | 2014-07-16 11:35:05 -0700 | [diff] [blame^] | 148 | if client.TimesCalled("Signature") != 2 { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 149 | t.Errorf("Cache was still used but TTL had passed. It should have been fetched again") |
| 150 | } |
| 151 | } |