Skip to content

Commit 994d046

Browse files
authored
fix(test): Clear caches in between test runs (serverpod#3727)
1 parent 0f8cb0a commit 994d046

File tree

8 files changed

+688
-0
lines changed

8 files changed

+688
-0
lines changed

packages/serverpod/lib/src/cache/caches.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ class Caches {
5353

5454
/// Cache used to automatically save cachable database queries.
5555
LocalCache get query => _query;
56+
57+
/// Clears all caches.
58+
Future<void> clear() async {
59+
await _local.clear();
60+
await _localPrio.clear();
61+
await _query.clear();
62+
}
5663
}

packages/serverpod_test/lib/src/with_serverpod.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ void Function(TestClosure<T>)
157157
await localTransactionManager.addSavepoint();
158158
}
159159

160+
await mainServerpodSession.caches.clear();
161+
160162
await GlobalStreamManager.closeAllStreams();
161163
});
162164

tests/serverpod_test_client/lib/src/protocol/client.dart

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3507,6 +3507,81 @@ class EndpointTestTools extends _i1.EndpointRef {
35073507
{},
35083508
{},
35093509
);
3510+
3511+
_i2.Future<void> putInLocalCache(
3512+
String key,
3513+
_i10.SimpleData data,
3514+
) =>
3515+
caller.callServerEndpoint<void>(
3516+
'testTools',
3517+
'putInLocalCache',
3518+
{
3519+
'key': key,
3520+
'data': data,
3521+
},
3522+
);
3523+
3524+
_i2.Future<_i10.SimpleData?> getFromLocalCache(String key) =>
3525+
caller.callServerEndpoint<_i10.SimpleData?>(
3526+
'testTools',
3527+
'getFromLocalCache',
3528+
{'key': key},
3529+
);
3530+
3531+
_i2.Future<void> putInLocalPrioCache(
3532+
String key,
3533+
_i10.SimpleData data,
3534+
) =>
3535+
caller.callServerEndpoint<void>(
3536+
'testTools',
3537+
'putInLocalPrioCache',
3538+
{
3539+
'key': key,
3540+
'data': data,
3541+
},
3542+
);
3543+
3544+
_i2.Future<_i10.SimpleData?> getFromLocalPrioCache(String key) =>
3545+
caller.callServerEndpoint<_i10.SimpleData?>(
3546+
'testTools',
3547+
'getFromLocalPrioCache',
3548+
{'key': key},
3549+
);
3550+
3551+
_i2.Future<void> putInQueryCache(
3552+
String key,
3553+
_i10.SimpleData data,
3554+
) =>
3555+
caller.callServerEndpoint<void>(
3556+
'testTools',
3557+
'putInQueryCache',
3558+
{
3559+
'key': key,
3560+
'data': data,
3561+
},
3562+
);
3563+
3564+
_i2.Future<_i10.SimpleData?> getFromQueryCache(String key) =>
3565+
caller.callServerEndpoint<_i10.SimpleData?>(
3566+
'testTools',
3567+
'getFromQueryCache',
3568+
{'key': key},
3569+
);
3570+
3571+
_i2.Future<void> putInLocalCacheWithGroup(
3572+
String key,
3573+
_i10.SimpleData data,
3574+
String group,
3575+
) =>
3576+
caller.callServerEndpoint<void>(
3577+
'testTools',
3578+
'putInLocalCacheWithGroup',
3579+
{
3580+
'key': key,
3581+
'data': data,
3582+
'group': group,
3583+
},
3584+
);
35103585
}
35113586

35123587
/// {@category Endpoint}

tests/serverpod_test_server/lib/src/endpoints/test_tools.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,39 @@ class TestToolsEndpoint extends Endpoint {
342342

343343
throw Exception();
344344
}
345+
346+
// Cache testing methods
347+
Future<void> putInLocalCache(
348+
Session session, String key, SimpleData data) async {
349+
await session.caches.local.put(key, data);
350+
}
351+
352+
Future<SimpleData?> getFromLocalCache(Session session, String key) async {
353+
return await session.caches.local.get<SimpleData>(key);
354+
}
355+
356+
Future<void> putInLocalPrioCache(
357+
Session session, String key, SimpleData data) async {
358+
await session.caches.localPrio.put(key, data);
359+
}
360+
361+
Future<SimpleData?> getFromLocalPrioCache(Session session, String key) async {
362+
return await session.caches.localPrio.get<SimpleData>(key);
363+
}
364+
365+
Future<void> putInQueryCache(
366+
Session session, String key, SimpleData data) async {
367+
await session.caches.query.put(key, data);
368+
}
369+
370+
Future<SimpleData?> getFromQueryCache(Session session, String key) async {
371+
return await session.caches.query.get<SimpleData>(key);
372+
}
373+
374+
Future<void> putInLocalCacheWithGroup(
375+
Session session, String key, SimpleData data, String group) async {
376+
await session.caches.local.put(key, data, group: group);
377+
}
345378
}
346379

347380
class AuthenticatedTestToolsEndpoint extends Endpoint {

tests/serverpod_test_server/lib/src/generated/endpoints.dart

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7291,6 +7291,169 @@ class Endpoints extends _i1.EndpointDispatch {
72917291
(endpoints['testTools'] as _i42.TestToolsEndpoint)
72927292
.addWillCloseListenerToSessionAndThrow(session),
72937293
),
7294+
'putInLocalCache': _i1.MethodConnector(
7295+
name: 'putInLocalCache',
7296+
params: {
7297+
'key': _i1.ParameterDescription(
7298+
name: 'key',
7299+
type: _i1.getType<String>(),
7300+
nullable: false,
7301+
),
7302+
'data': _i1.ParameterDescription(
7303+
name: 'data',
7304+
type: _i1.getType<_i50.SimpleData>(),
7305+
nullable: false,
7306+
),
7307+
},
7308+
call: (
7309+
_i1.Session session,
7310+
Map<String, dynamic> params,
7311+
) async =>
7312+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7313+
.putInLocalCache(
7314+
session,
7315+
params['key'],
7316+
params['data'],
7317+
),
7318+
),
7319+
'getFromLocalCache': _i1.MethodConnector(
7320+
name: 'getFromLocalCache',
7321+
params: {
7322+
'key': _i1.ParameterDescription(
7323+
name: 'key',
7324+
type: _i1.getType<String>(),
7325+
nullable: false,
7326+
)
7327+
},
7328+
call: (
7329+
_i1.Session session,
7330+
Map<String, dynamic> params,
7331+
) async =>
7332+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7333+
.getFromLocalCache(
7334+
session,
7335+
params['key'],
7336+
),
7337+
),
7338+
'putInLocalPrioCache': _i1.MethodConnector(
7339+
name: 'putInLocalPrioCache',
7340+
params: {
7341+
'key': _i1.ParameterDescription(
7342+
name: 'key',
7343+
type: _i1.getType<String>(),
7344+
nullable: false,
7345+
),
7346+
'data': _i1.ParameterDescription(
7347+
name: 'data',
7348+
type: _i1.getType<_i50.SimpleData>(),
7349+
nullable: false,
7350+
),
7351+
},
7352+
call: (
7353+
_i1.Session session,
7354+
Map<String, dynamic> params,
7355+
) async =>
7356+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7357+
.putInLocalPrioCache(
7358+
session,
7359+
params['key'],
7360+
params['data'],
7361+
),
7362+
),
7363+
'getFromLocalPrioCache': _i1.MethodConnector(
7364+
name: 'getFromLocalPrioCache',
7365+
params: {
7366+
'key': _i1.ParameterDescription(
7367+
name: 'key',
7368+
type: _i1.getType<String>(),
7369+
nullable: false,
7370+
)
7371+
},
7372+
call: (
7373+
_i1.Session session,
7374+
Map<String, dynamic> params,
7375+
) async =>
7376+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7377+
.getFromLocalPrioCache(
7378+
session,
7379+
params['key'],
7380+
),
7381+
),
7382+
'putInQueryCache': _i1.MethodConnector(
7383+
name: 'putInQueryCache',
7384+
params: {
7385+
'key': _i1.ParameterDescription(
7386+
name: 'key',
7387+
type: _i1.getType<String>(),
7388+
nullable: false,
7389+
),
7390+
'data': _i1.ParameterDescription(
7391+
name: 'data',
7392+
type: _i1.getType<_i50.SimpleData>(),
7393+
nullable: false,
7394+
),
7395+
},
7396+
call: (
7397+
_i1.Session session,
7398+
Map<String, dynamic> params,
7399+
) async =>
7400+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7401+
.putInQueryCache(
7402+
session,
7403+
params['key'],
7404+
params['data'],
7405+
),
7406+
),
7407+
'getFromQueryCache': _i1.MethodConnector(
7408+
name: 'getFromQueryCache',
7409+
params: {
7410+
'key': _i1.ParameterDescription(
7411+
name: 'key',
7412+
type: _i1.getType<String>(),
7413+
nullable: false,
7414+
)
7415+
},
7416+
call: (
7417+
_i1.Session session,
7418+
Map<String, dynamic> params,
7419+
) async =>
7420+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7421+
.getFromQueryCache(
7422+
session,
7423+
params['key'],
7424+
),
7425+
),
7426+
'putInLocalCacheWithGroup': _i1.MethodConnector(
7427+
name: 'putInLocalCacheWithGroup',
7428+
params: {
7429+
'key': _i1.ParameterDescription(
7430+
name: 'key',
7431+
type: _i1.getType<String>(),
7432+
nullable: false,
7433+
),
7434+
'data': _i1.ParameterDescription(
7435+
name: 'data',
7436+
type: _i1.getType<_i50.SimpleData>(),
7437+
nullable: false,
7438+
),
7439+
'group': _i1.ParameterDescription(
7440+
name: 'group',
7441+
type: _i1.getType<String>(),
7442+
nullable: false,
7443+
),
7444+
},
7445+
call: (
7446+
_i1.Session session,
7447+
Map<String, dynamic> params,
7448+
) async =>
7449+
(endpoints['testTools'] as _i42.TestToolsEndpoint)
7450+
.putInLocalCacheWithGroup(
7451+
session,
7452+
params['key'],
7453+
params['data'],
7454+
params['group'],
7455+
),
7456+
),
72947457
'returnsSessionIdFromStream': _i1.MethodStreamConnector(
72957458
name: 'returnsSessionIdFromStream',
72967459
params: {},

tests/serverpod_test_server/lib/src/generated/protocol.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ testTools:
417417
- logMessageWithSession:
418418
- addWillCloseListenerToSessionAndThrow:
419419
- addWillCloseListenerToSessionIntStreamMethodAndThrow:
420+
- putInLocalCache:
421+
- getFromLocalCache:
422+
- putInLocalPrioCache:
423+
- getFromLocalPrioCache:
424+
- putInQueryCache:
425+
- getFromQueryCache:
426+
- putInLocalCacheWithGroup:
420427
authenticatedTestTools:
421428
- returnsString:
422429
- returnsStream:

0 commit comments

Comments
 (0)