blob: f0caf010bd7f79316d4e9eaa740eb56917140d4b [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package lib
2
3import (
4 "reflect"
5 "testing"
6
7 mocks_ipc "veyron/runtimes/google/testing/mocks/ipc"
Jiri Simsa5293dcb2014-05-10 09:56:38 -07008 "veyron2/ipc"
Matt Rosencrantzf5afcaf2014-06-02 11:31:22 -07009 "veyron2/rt"
Todd Wang0ecdd7a2014-07-14 16:24:38 -070010 "veyron2/vdl/vdlutil"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070011 "veyron2/wiretype"
12)
13
14const (
15 name = "/veyron/name"
16)
17
Shyam Jayaraman72718c92014-07-16 11:35:05 -070018func init() {
19 rt.Init()
20}
21
Jiri Simsa5293dcb2014-05-10 09:56:38 -070022func expectedSignature() ipc.ServiceSignature {
23 return ipc.ServiceSignature{
24 Methods: make(map[string]ipc.MethodSignature),
Todd Wang0ecdd7a2014-07-14 16:24:38 -070025 TypeDefs: []vdlutil.Any{
Jiri Simsa5293dcb2014-05-10 09:56:38 -070026 wiretype.NamedPrimitiveType{
Todd Wang0ecdd7a2014-07-14 16:24:38 -070027 Name: "veyron2/vdlutil.AnyData",
Jiri Simsa5293dcb2014-05-10 09:56:38 -070028 Type: wiretype.TypeIDInterface,
29 },
30 },
31 }
32}
33
34func client() *mocks_ipc.SimpleMockClient {
35 return mocks_ipc.NewSimpleClient(
36 map[string][]interface{}{
Shyam Jayaraman72718c92014-07-16 11:35:05 -070037 "Signature": []interface{}{expectedSignature(), nil},
Jiri Simsa5293dcb2014-05-10 09:56:38 -070038 },
39 )
40}
41
42func 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
61func 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
81func TestFetching(t *testing.T) {
Shyam Jayaraman72718c92014-07-16 11:35:05 -070082 sm := NewSignatureManager()
83 got, err := sm.Signature(rt.R().NewContext(), name, client())
Jiri Simsa5293dcb2014-05-10 09:56:38 -070084 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
92func TestThatCachedAfterFetching(t *testing.T) {
Shyam Jayaraman72718c92014-07-16 11:35:05 -070093 sm := NewSignatureManager().(*signatureManager)
94 sig, _ := sm.Signature(rt.R().NewContext(), name, client())
Jiri Simsa5293dcb2014-05-10 09:56:38 -070095 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
103func TestThatCacheIsUsed(t *testing.T) {
104 client := client()
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700105 sm := NewSignatureManager()
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700106
107 // call twice
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700108 sm.Signature(rt.R().NewContext(), name, client)
109 sm.Signature(rt.R().NewContext(), name, client)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700110
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 Jayaraman72718c92014-07-16 11:35:05 -0700113 if client.TimesCalled("Signature") != 1 {
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700114 t.Errorf("Signature cache was not used for the second call")
115 }
116}
117
118func TestThatLastAccessedGetUpdated(t *testing.T) {
119 client := client()
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700120 sm := NewSignatureManager().(*signatureManager)
121 sm.Signature(rt.R().NewContext(), name, client)
Ali Ghassemi598309d2014-05-27 08:57:53 -0700122 // 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 Simsa5293dcb2014-05-10 09:56:38 -0700125 prevAccess := sm.cache[name].lastAccessed
126
127 // access again
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700128 sm.Signature(rt.R().NewContext(), name, client)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700129 newAccess := sm.cache[name].lastAccessed
130
131 if !newAccess.After(prevAccess) {
132 t.Errorf("LastAccessed was not updated for cache entry")
133 }
134}
135
136func TestThatTTLExpires(t *testing.T) {
137 client := client()
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700138 sm := NewSignatureManager().(*signatureManager)
139 sm.Signature(rt.R().NewContext(), name, client)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700140
141 // make last accessed go over the ttl
Shyam Jayaramanc0b0d8a2014-05-13 11:31:00 -0700142 sm.cache[name].lastAccessed = sm.cache[name].lastAccessed.Add(-2 * ttl)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700143
144 // make a second call
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700145 sm.Signature(rt.R().NewContext(), name, client)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700146
147 // expect number of calls to Signature method of client to be 2 since cache should have expired
Shyam Jayaraman72718c92014-07-16 11:35:05 -0700148 if client.TimesCalled("Signature") != 2 {
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700149 t.Errorf("Cache was still used but TTL had passed. It should have been fetched again")
150 }
151}