Return-Path: X-Original-To: apmail-couchdb-commits-archive@www.apache.org Delivered-To: apmail-couchdb-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5ADFB11B60 for ; Tue, 26 Aug 2014 20:59:53 +0000 (UTC) Received: (qmail 27941 invoked by uid 500); 26 Aug 2014 20:59:53 -0000 Delivered-To: apmail-couchdb-commits-archive@couchdb.apache.org Received: (qmail 27761 invoked by uid 500); 26 Aug 2014 20:59:53 -0000 Mailing-List: contact commits-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@couchdb.apache.org Delivered-To: mailing list commits@couchdb.apache.org Received: (qmail 27737 invoked by uid 99); 26 Aug 2014 20:59:53 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 26 Aug 2014 20:59:53 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id EDA509CEEB3; Tue, 26 Aug 2014 20:59:52 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: chewbranca@apache.org To: commits@couchdb.apache.org Date: Tue, 26 Aug 2014 20:59:53 -0000 Message-Id: <5c1a959808834558872af5b54181b415@git.apache.org> In-Reply-To: <2f010890076e4986acd86778ee57ec89@git.apache.org> References: <2f010890076e4986acd86778ee57ec89@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [02/50] [abbrv] couch commit: updated refs/heads/1963-eunit-bigcouch to 0efdc71 Port 041-uuid.t etap test suite to eunit Config files are removed in favor of using couch_config API instead. Project: http://git-wip-us.apache.org/repos/asf/couchdb-couch/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-couch/commit/bf86b7da Tree: http://git-wip-us.apache.org/repos/asf/couchdb-couch/tree/bf86b7da Diff: http://git-wip-us.apache.org/repos/asf/couchdb-couch/diff/bf86b7da Branch: refs/heads/1963-eunit-bigcouch Commit: bf86b7da92ef67bba2920a2b73bf17683b784696 Parents: 6352b7f Author: Alexander Shorin Authored: Sat May 17 04:23:25 2014 +0400 Committer: Russell Branca Committed: Tue Aug 26 13:59:29 2014 -0700 ---------------------------------------------------------------------- test/couchdb/couch_uuids_tests.erl | 161 ++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-couch/blob/bf86b7da/test/couchdb/couch_uuids_tests.erl ---------------------------------------------------------------------- diff --git a/test/couchdb/couch_uuids_tests.erl b/test/couchdb/couch_uuids_tests.erl new file mode 100644 index 0000000..ea1d034 --- /dev/null +++ b/test/couchdb/couch_uuids_tests.erl @@ -0,0 +1,161 @@ +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +-module(couch_uuids_tests). + +-include("couch_eunit.hrl"). + +-define(TIMEOUT_S, 20). + + +setup() -> + {ok, Pid} = couch_config:start_link(?CONFIG_CHAIN), + erlang:monitor(process, Pid), + couch_uuids:start(), + Pid. + +setup(Opts) -> + Pid = setup(), + lists:foreach( + fun({Option, Value}) -> + couch_config:set("uuids", Option, Value, false) + end, Opts), + Pid. + +teardown(Pid) -> + couch_uuids:stop(), + couch_config:stop(), + receive + {'DOWN', _, _, Pid, _} -> ok + after + 1000 -> throw({timeout_error, config_stop}) + end. + +teardown(_, Pid) -> + teardown(Pid). + + +default_test_() -> + { + "Default UUID algorithm", + { + setup, + fun setup/0, fun teardown/1, + fun should_be_unique/1 + } + }. + +sequential_test_() -> + Opts = [{"algorithm", "sequential"}], + Cases = [ + fun should_be_unique/2, + fun should_increment_monotonically/2, + fun should_rollover/2 + ], + { + "UUID algorithm: sequential", + { + foreachx, + fun setup/1, fun teardown/2, + [{Opts, Fun} || Fun <- Cases] + } + }. + +utc_test_() -> + Opts = [{"algorithm", "utc_random"}], + Cases = [ + fun should_be_unique/2, + fun should_increment_monotonically/2 + ], + { + "UUID algorithm: utc_random", + { + foreachx, + fun setup/1, fun teardown/2, + [{Opts, Fun} || Fun <- Cases] + } + }. + +utc_id_suffix_test_() -> + Opts = [{"algorithm", "utc_id"}, {"utc_id_suffix", "bozo"}], + Cases = [ + fun should_be_unique/2, + fun should_increment_monotonically/2, + fun should_preserve_suffix/2 + ], + { + "UUID algorithm: utc_id", + { + foreachx, + fun setup/1, fun teardown/2, + [{Opts, Fun} || Fun <- Cases] + } + }. + + +should_be_unique() -> + %% this one may really runs for too long on slow hosts + {timeout, ?TIMEOUT_S, ?_assert(test_unique(10000, [couch_uuids:new()]))}. +should_be_unique(_) -> + should_be_unique(). +should_be_unique(_, _) -> + should_be_unique(). + +should_increment_monotonically(_, _) -> + ?_assert(couch_uuids:new() < couch_uuids:new()). + +should_rollover(_, _) -> + ?_test(begin + UUID = binary_to_list(couch_uuids:new()), + Prefix = element(1, lists:split(26, UUID)), + N = gen_until_pref_change(Prefix, 0), + ?assert(N >= 5000 andalso N =< 11000) + end). + +should_preserve_suffix(_, _) -> + ?_test(begin + UUID = binary_to_list(couch_uuids:new()), + Suffix = get_suffix(UUID), + ?assert(test_same_suffix(10000, Suffix)) + end). + + +test_unique(0, _) -> + true; +test_unique(N, UUIDs) -> + UUID = couch_uuids:new(), + ?assertNot(lists:member(UUID, UUIDs)), + test_unique(N - 1, [UUID| UUIDs]). + +get_prefix(UUID) -> + element(1, lists:split(26, binary_to_list(UUID))). + +gen_until_pref_change(_, Count) when Count > 8251 -> + Count; +gen_until_pref_change(Prefix, N) -> + case get_prefix(couch_uuids:new()) of + Prefix -> gen_until_pref_change(Prefix, N + 1); + _ -> N + end. + +get_suffix(UUID) when is_binary(UUID) -> + get_suffix(binary_to_list(UUID)); +get_suffix(UUID) -> + element(2, lists:split(14, UUID)). + +test_same_suffix(0, _) -> + true; +test_same_suffix(N, Suffix) -> + case get_suffix(couch_uuids:new()) of + Suffix -> test_same_suffix(N - 1, Suffix); + _ -> false + end.