Skip to content

Commit 89d42b8

Browse files
committed
khepri_machine: Fix comparison between opaque khepri_machine:state() terms
[Why] `khepri_machine:state()` can be either a `state_v0()` or a `state_v1()` which are both opaque types. Therefore, they can't simply be compared with =:= because it violates the contract that a e.g. a `state_v0()` can't be compared to a `state_v1()` without knowing their internals. This was reported by Dialyzer in Erlang/OTP 28. [How] We add an assert function to both `khepri_machine` and `khepri_machine_v0`. They can inspect their respective version of the state because they define it.
1 parent 15f93dc commit 89d42b8

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/khepri_machine.erl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,10 @@ readonly_transaction(StoreId, Fun, Args, Options)
520520
%% It is a read-only transaction, therefore we assert that
521521
%% the state is unchanged and that there are no side
522522
%% effects.
523-
{State, Ret, []} = khepri_tx_adv:run(
524-
State, Fun, Args, false,
525-
Meta),
523+
{State1, Ret, []} = khepri_tx_adv:run(
524+
State, Fun, Args, false,
525+
Meta),
526+
assert_equal(State, State1),
526527
Ret
527528
end,
528529
case process_query(StoreId, Query, Options) of
@@ -537,9 +538,10 @@ readonly_transaction(StoreId, PathPattern, Args, Options)
537538
%% It is a read-only transaction, therefore we assert that
538539
%% the state is unchanged and that there are no side
539540
%% effects.
540-
{State, Ret, []} = locate_sproc_and_execute_tx(
541-
State, PathPattern, Args, false,
542-
Meta),
541+
{State1, Ret, []} = locate_sproc_and_execute_tx(
542+
State, PathPattern, Args, false,
543+
Meta),
544+
assert_equal(State, State1),
543545
Ret
544546
end,
545547
case process_query(StoreId, Query, Options) of
@@ -2634,6 +2636,16 @@ get_store_id(State) ->
26342636
#config{store_id = StoreId} = get_config(State),
26352637
StoreId.
26362638

2639+
-spec assert_equal(State1, State2) -> ok when
2640+
State1 :: khepri_machine:state(),
2641+
State2 :: khepri_machine:state().
2642+
2643+
assert_equal(#khepri_machine{} = State1, #khepri_machine{} = State2) ->
2644+
?assertEqual(State1, State2),
2645+
ok;
2646+
assert_equal(State1, State2) ->
2647+
khepri_machine_v0:assert_equal(State1, State2).
2648+
26372649
-ifdef(TEST).
26382650
-spec make_virgin_state(Params) -> State when
26392651
Params :: khepri_machine:machine_init_args(),

src/khepri_machine_v0.erl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
-module(khepri_machine_v0).
1515

16+
-include_lib("stdlib/include/assert.hrl").
17+
1618
-include("src/khepri_machine.hrl").
1719

1820
-export([init/1]).
@@ -25,6 +27,7 @@
2527
get_emitted_triggers/1, set_emitted_triggers/2,
2628
get_projections/1, set_projections/2,
2729
get_metrics/1, set_metrics/2,
30+
assert_equal/2,
2831
state_to_list/1]).
2932

3033
-record(khepri_machine,
@@ -191,6 +194,7 @@ get_metrics(#khepri_machine{metrics = Metrics}) ->
191194
set_metrics(#khepri_machine{} = State, Metrics) ->
192195
State#khepri_machine{metrics = Metrics}.
193196

197+
194198
-spec state_to_list(State) -> Fields when
195199
State :: khepri_machine_v0:state(),
196200
Fields :: [any()].
@@ -200,3 +204,11 @@ set_metrics(#khepri_machine{} = State, Metrics) ->
200204

201205
state_to_list(#khepri_machine{} = State) ->
202206
tuple_to_list(State).
207+
208+
-spec assert_equal(State1, State2) -> ok when
209+
State1 :: khepri_machine:state(),
210+
State2 :: khepri_machine:state().
211+
212+
assert_equal(#khepri_machine{} = State1, #khepri_machine{} = State2) ->
213+
?assertEqual(State1, State2),
214+
ok.

0 commit comments

Comments
 (0)