a4cf473ee9
* chore: run go-generate See https://github.com/ooni/probe/issues/1468 * chore: update all the dependencies Unclear to me why `go get -u -v ./...` did not actually update all of them and I needed to spell out each of them and force to update by going `go get -u -v $pkg@latest` ¯\_(ツ)_/¯. Part of https://github.com/ooni/probe/issues/1468 * fix(c/o/i/d/actions_test.go): ensure we check for return value Part of https://github.com/ooni/probe/issues/1468 * chore: update the user agents we use See https://github.com/ooni/probe/issues/1468 * chore: set version to 3.10.0-beta.3 See https://github.com/ooni/probe/issues/1468 * chore: use probe-assets v0.3.1 Part of https://github.com/ooni/probe/issues/1468
223 lines
5.0 KiB
Go
223 lines
5.0 KiB
Go
// Code generated by go generate; DO NOT EDIT.
|
|
// 2021-05-12 09:15:02.497717446 +0200 CEST m=+0.000113904
|
|
|
|
package ooapi
|
|
|
|
//go:generate go run ./internal/generator -file caching_test.go
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/ooapi/apimodel"
|
|
)
|
|
|
|
func TestCachesimpleMeasurementMetaAPISuccess(t *testing.T) {
|
|
ff := &fakeFill{}
|
|
var expect *apimodel.MeasurementMetaResponse
|
|
ff.fill(&expect)
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
API: &FakeMeasurementMetaAPI{
|
|
Response: expect,
|
|
},
|
|
KVStore: &MemKVStore{},
|
|
}
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
ctx := context.Background()
|
|
resp, err := cache.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if diff := cmp.Diff(expect, resp); diff != "" {
|
|
t.Fatal(diff)
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPIWriteCacheError(t *testing.T) {
|
|
errMocked := errors.New("mocked error")
|
|
ff := &fakeFill{}
|
|
var expect *apimodel.MeasurementMetaResponse
|
|
ff.fill(&expect)
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
API: &FakeMeasurementMetaAPI{
|
|
Response: expect,
|
|
},
|
|
KVStore: &FakeKVStore{SetError: errMocked},
|
|
}
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
ctx := context.Background()
|
|
resp, err := cache.Call(ctx, req)
|
|
if !errors.Is(err, errMocked) {
|
|
t.Fatal("not the error we expected", err)
|
|
}
|
|
if resp != nil {
|
|
t.Fatal("expected nil response")
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPIFailureWithNoCache(t *testing.T) {
|
|
errMocked := errors.New("mocked error")
|
|
ff := &fakeFill{}
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
API: &FakeMeasurementMetaAPI{
|
|
Err: errMocked,
|
|
},
|
|
KVStore: &MemKVStore{},
|
|
}
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
ctx := context.Background()
|
|
resp, err := cache.Call(ctx, req)
|
|
if !errors.Is(err, errMocked) {
|
|
t.Fatal("not the error we expected", err)
|
|
}
|
|
if resp != nil {
|
|
t.Fatal("expected nil response")
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPIFailureWithPreviousCache(t *testing.T) {
|
|
ff := &fakeFill{}
|
|
var expect *apimodel.MeasurementMetaResponse
|
|
ff.fill(&expect)
|
|
fakeapi := &FakeMeasurementMetaAPI{
|
|
Response: expect,
|
|
}
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
API: fakeapi,
|
|
KVStore: &MemKVStore{},
|
|
}
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
ctx := context.Background()
|
|
// first pass with no error at all
|
|
// use a separate scope to be sure we avoid mistakes
|
|
{
|
|
resp, err := cache.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if diff := cmp.Diff(expect, resp); diff != "" {
|
|
t.Fatal(diff)
|
|
}
|
|
}
|
|
// second pass with failure
|
|
errMocked := errors.New("mocked error")
|
|
fakeapi.Err = errMocked
|
|
fakeapi.Response = nil
|
|
resp2, err := cache.Call(ctx, req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if resp2 == nil {
|
|
t.Fatal("expected non-nil response")
|
|
}
|
|
if diff := cmp.Diff(expect, resp2); diff != "" {
|
|
t.Fatal(diff)
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPISetcacheWithEncodeError(t *testing.T) {
|
|
ff := &fakeFill{}
|
|
errMocked := errors.New("mocked error")
|
|
var in []cacheEntryForMeasurementMetaAPI
|
|
ff.fill(&in)
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
GobCodec: &FakeCodec{EncodeErr: errMocked},
|
|
}
|
|
err := cache.setcache(in)
|
|
if !errors.Is(err, errMocked) {
|
|
t.Fatal("not the error we expected", err)
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPIReadCacheNotFound(t *testing.T) {
|
|
ff := &fakeFill{}
|
|
var incache []cacheEntryForMeasurementMetaAPI
|
|
ff.fill(&incache)
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
KVStore: &MemKVStore{},
|
|
}
|
|
err := cache.setcache(incache)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
out, err := cache.readcache(req)
|
|
if !errors.Is(err, errCacheNotFound) {
|
|
t.Fatal("not the error we expected", err)
|
|
}
|
|
if out != nil {
|
|
t.Fatal("expected nil here")
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPIWriteCacheDuplicate(t *testing.T) {
|
|
ff := &fakeFill{}
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
var resp1 *apimodel.MeasurementMetaResponse
|
|
ff.fill(&resp1)
|
|
var resp2 *apimodel.MeasurementMetaResponse
|
|
ff.fill(&resp2)
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
KVStore: &MemKVStore{},
|
|
}
|
|
err := cache.writecache(req, resp1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err = cache.writecache(req, resp2)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := cache.readcache(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out == nil {
|
|
t.Fatal("expected non-nil here")
|
|
}
|
|
if diff := cmp.Diff(resp2, out); diff != "" {
|
|
t.Fatal(diff)
|
|
}
|
|
}
|
|
|
|
func TestCachesimpleMeasurementMetaAPICacheSizeLimited(t *testing.T) {
|
|
ff := &fakeFill{}
|
|
cache := &withCacheMeasurementMetaAPI{
|
|
KVStore: &MemKVStore{},
|
|
}
|
|
var prev int
|
|
for {
|
|
var req *apimodel.MeasurementMetaRequest
|
|
ff.fill(&req)
|
|
var resp *apimodel.MeasurementMetaResponse
|
|
ff.fill(&resp)
|
|
err := cache.writecache(req, resp)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := cache.getcache()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(out) > prev {
|
|
prev = len(out)
|
|
continue
|
|
}
|
|
break
|
|
}
|
|
}
|