Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add a property-test and refactor

  • Loading branch information...
commit 6a8499a4e41772101ec1c3904857c8b315092c96 1 parent a2d57da
Yosuke Hara yosukehara authored
1  .gitignore
... ... @@ -1,3 +1,4 @@
1 1 .eunit/
2 2 deps/
3 3 ebin/
  4 +test/*.beam
2  src/leo_backend_db_api.erl
@@ -298,7 +298,7 @@ start_app() ->
298 298 Module = leo_backend_db,
299 299 case application:start(Module) of
300 300 ok ->
301   - ?ETS_TABLE_NAME = ets:new(?ETS_TABLE_NAME, [named_table, public, {read_concurrency, true}]),
  301 + catch ets:new(?ETS_TABLE_NAME, [named_table, public, {read_concurrency, true}]),
302 302 ok;
303 303 {error, {already_started, Module}} ->
304 304 ok;
2  test/leo_backend_db_api_tests.erl
@@ -2,7 +2,7 @@
2 2 %%
3 3 %% Leo Backend DB
4 4 %%
5   -%% Copyright (c) 2012
  5 +%% Copyright (c) 2012 Rakuten, Inc.
6 6 %%
7 7 %% This file is provided to you under the Apache License,
8 8 %% Version 2.0 (the "License"); you may not use this file
127 test/leo_backend_db_prop.erl
... ... @@ -0,0 +1,127 @@
  1 +%%====================================================================
  2 +%%
  3 +%% Leo Backend DB
  4 +%%
  5 +%% Copyright (c) 2012 Rakuten, Inc.
  6 +%%
  7 +%% This file is provided to you under the Apache License,
  8 +%% Version 2.0 (the "License"); you may not use this file
  9 +%% except in compliance with the License. You may obtain
  10 +%% a copy of the License at
  11 +%%
  12 +%% http://www.apache.org/licenses/LICENSE-2.0
  13 +%%
  14 +%% Unless required by applicable law or agreed to in writing,
  15 +%% software distributed under the License is distributed on an
  16 +%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17 +%% KIND, either express or implied. See the License for the
  18 +%% specific language governing permissions and limitations
  19 +%% under the License.
  20 +%%
  21 +%% -------------------------------------------------------------------
  22 +%% Leo Backend DB - Property TEST
  23 +%% @doc
  24 +%% @end
  25 +%%====================================================================
  26 +-module(leo_backend_db_prop).
  27 +-include_lib("proper/include/proper.hrl").
  28 +-include_lib("eunit/include/eunit.hrl").
  29 +
  30 +-behaviour('proper_statem').
  31 +
  32 +-export([initial_state/0,
  33 + command/1,
  34 + precondition/2,
  35 + next_state/3,
  36 + postcondition/3]).
  37 +
  38 +
  39 +-type db_type() :: 'bitcask' | 'leveldb'.
  40 +
  41 +-record(state, {stored = [] :: [string()], %% list of objects stored in DB
  42 + type :: db_type()
  43 + }).
  44 +
  45 +
  46 +-define(INSTANCE_NAME, 'backend_db').
  47 +-define(DB_PATH, "./db/temp").
  48 +-define(NUM_OF_PROCS, 3).
  49 +-define(KEYS, lists:map(fun(N) ->
  50 + list_to_binary(
  51 + integer_to_list(N))
  52 + end, lists:seq(0, 9))).
  53 +
  54 +
  55 +%% Utils
  56 +%%
  57 +key() ->
  58 + frequency([{1, elements(?KEYS)}]).
  59 +
  60 +value() ->
  61 + frequency([{1, elements([<<"a">>, <<"b">>, <<"c">>, <<"d">>, <<"e">>])}]).
  62 +
  63 +
  64 +%% Property TEST
  65 +%%
  66 +prop_db() ->
  67 + ?FORALL(Type, noshrink(db_type()),
  68 + ?FORALL(Cmds, commands(?MODULE, initial_state(Type)),
  69 + begin
  70 + leo_backend_db_api:new(?INSTANCE_NAME, ?NUM_OF_PROCS, Type, ?DB_PATH),
  71 + {H,S,Res} = run_commands(?MODULE, Cmds),
  72 + ?debugVal({H,S,Res}),
  73 +
  74 + leo_backend_db_api:stop(?INSTANCE_NAME),
  75 + application:stop(leo_backend_db),
  76 + os:cmd("rm -rf ./db"),
  77 +
  78 + ?WHENFAIL(
  79 + io:format("History: ~p\nState: ~p\nRes: ~p\n", [H,S,Res]),
  80 + collect(Type, Res =:= ok))
  81 + end)).
  82 +
  83 +
  84 +
  85 +%% Initialize state
  86 +%%
  87 +initial_state() ->
  88 + #state{type = set}.
  89 +initial_state(Type) ->
  90 + #state{type = Type}.
  91 +
  92 +
  93 +%% Command
  94 +%%
  95 +command(_S) ->
  96 + Cmd0 = [{call, leo_backend_db_api, put, [?INSTANCE_NAME, key(), value()]},
  97 + {call, leo_backend_db_api, get, [?INSTANCE_NAME, key()]},
  98 + {call, leo_backend_db_api, delete, [?INSTANCE_NAME, key()]}],
  99 + oneof(Cmd0).
  100 +
  101 +
  102 +%% Pre-Condition
  103 +%%
  104 +precondition(_S, {call,_,_,_}) ->
  105 + true.
  106 +
  107 +
  108 +%% Next-State
  109 +%%
  110 +next_state(S, _V, {call,_,put,[_Instance, Key,_Object]}) ->
  111 + case proplists:is_defined(Key, S#state.stored) of
  112 + false ->
  113 + S#state{stored = S#state.stored ++ [Key]};
  114 + true ->
  115 + S
  116 + end;
  117 +
  118 +next_state(S, _V, {call,_,delete,[_Instance, Key]}) ->
  119 + S#state{stored=proplists:delete(Key, S#state.stored)};
  120 +next_state(S, _V, {call,_,_,_}) ->
  121 + S.
  122 +
  123 +
  124 +%% Post-Condition
  125 +%%
  126 +postcondition(_,_,_) ->
  127 + true.

0 comments on commit 6a8499a

Please sign in to comment.
Something went wrong with that request. Please try again.