|
7 | 7 | "errors" |
8 | 8 | "fmt" |
9 | 9 | "testing" |
| 10 | + "time" |
10 | 11 |
|
11 | 12 | "google.golang.org/protobuf/proto" |
12 | 13 |
|
@@ -104,3 +105,98 @@ func TestBackendGetterV2Bad(t *testing.T) { |
104 | 105 | } |
105 | 106 | } |
106 | 107 | } |
| 108 | + |
| 109 | +// Test of common-good-case |
| 110 | +func TestBackendGetterWithInfoGood(t *testing.T) { |
| 111 | + expiry := time.Now().Add(time.Minute * 97) |
| 112 | + beGood := func(ctx context.Context, key string) (*testpbv2.TestMessage, galaxycache.BackendGetInfo, error) { |
| 113 | + return &testpbv2.TestMessage{ |
| 114 | + Name: proto.String("TestName"), |
| 115 | + City: proto.String("TestCity"), |
| 116 | + }, galaxycache.BackendGetInfo{Expiration: expiry}, nil |
| 117 | + } |
| 118 | + |
| 119 | + be := protocodec.BackendGetterWithInfo(beGood) |
| 120 | + |
| 121 | + ctx := context.Background() |
| 122 | + |
| 123 | + // test with a proto codec passed (local common-case) |
| 124 | + { |
| 125 | + pc := protocodec.NewV2[testpbv2.TestMessage]() |
| 126 | + |
| 127 | + if bgInfo, getErr := be.GetWithInfo(ctx, "foobar", &pc); getErr != nil { |
| 128 | + t.Errorf("noop Get call failed: %s", getErr) |
| 129 | + } else if !bgInfo.Expiration.Equal(expiry) { |
| 130 | + t.Errorf("unexpected expiration: %s; wanted %s", bgInfo.Expiration, expiry) |
| 131 | + } |
| 132 | + |
| 133 | + pv := pc.Get() |
| 134 | + if pv.City == nil || *pv.City != "TestCity" { |
| 135 | + t.Errorf("unexpected value for City: %v", pv.City) |
| 136 | + } |
| 137 | + if pv.Name == nil || *pv.Name != "TestName" { |
| 138 | + t.Errorf("unexpected value for Name: %v", pv.Name) |
| 139 | + } |
| 140 | + } |
| 141 | + // test with a ByteCodec to exercise the common-case when a remote-fetch is done |
| 142 | + { |
| 143 | + c := galaxycache.ByteCodec{} |
| 144 | + |
| 145 | + if bgInfo, getErr := be.GetWithInfo(ctx, "foobar", &c); getErr != nil { |
| 146 | + t.Errorf("noop Get call failed: %s", getErr) |
| 147 | + } else if !bgInfo.Expiration.Equal(expiry) { |
| 148 | + t.Errorf("unexpected expiration: %s; wanted %s", bgInfo.Expiration, expiry) |
| 149 | + } |
| 150 | + |
| 151 | + if len(c) < len("TestName")+len("TestCity") { |
| 152 | + t.Errorf("marshaled bytes too short (less than sum of two string fields)") |
| 153 | + } |
| 154 | + |
| 155 | + pc := protocodec.NewV2[testpbv2.TestMessage]() |
| 156 | + |
| 157 | + if umErr := pc.UnmarshalBinary([]byte(c)); umErr != nil { |
| 158 | + t.Errorf("failed to unmarshal bytes: %s", umErr) |
| 159 | + } |
| 160 | + |
| 161 | + pv := pc.Get() |
| 162 | + if pv.City == nil || *pv.City != "TestCity" { |
| 163 | + t.Errorf("unexpected value for City: %v", pv.City) |
| 164 | + } |
| 165 | + if pv.Name == nil || *pv.Name != "TestName" { |
| 166 | + t.Errorf("unexpected value for Name: %v", pv.Name) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestBackendGetterWithInfoBad(t *testing.T) { |
| 172 | + sentinel := errors.New("sentinel error") |
| 173 | + |
| 174 | + beErrorer := func(ctx context.Context, key string) (*testpbv2.TestMessage, galaxycache.BackendGetInfo, error) { |
| 175 | + return nil, galaxycache.BackendGetInfo{}, fmt.Errorf("error: %w", sentinel) |
| 176 | + } |
| 177 | + |
| 178 | + be := protocodec.BackendGetterWithInfo(beErrorer) |
| 179 | + |
| 180 | + ctx := context.Background() |
| 181 | + |
| 182 | + // test with a proto codec passed (local common-case) |
| 183 | + { |
| 184 | + pc := protocodec.NewV2[testpbv2.TestMessage]() |
| 185 | + |
| 186 | + if _, getErr := be.GetWithInfo(ctx, "foobar", &pc); getErr == nil { |
| 187 | + t.Errorf("noop Get call didn't fail") |
| 188 | + } else if !errors.Is(getErr, sentinel) { |
| 189 | + t.Errorf("Error from Get did not wrap/equal sentinel") |
| 190 | + } |
| 191 | + } |
| 192 | + // test with a ByteCodec to exercise the common-case when a remote-fetch is done |
| 193 | + { |
| 194 | + c := galaxycache.ByteCodec{} |
| 195 | + |
| 196 | + if _, getErr := be.GetWithInfo(ctx, "foobar", &c); getErr == nil { |
| 197 | + t.Errorf("noop Get call didn't fail") |
| 198 | + } else if !errors.Is(getErr, sentinel) { |
| 199 | + t.Errorf("Error from Get did not wrap/equal sentinel") |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments