nickva commented on code in PR #5603: URL: https://github.com/apache/couchdb/pull/5603#discussion_r2267357121
########## src/couch/test/eunit/couch_time_seq_tests.erl: ########## @@ -0,0 +1,355 @@ +% 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_time_seq_tests). + +-ifdef(WITH_PROPER). +-include_lib("couch/include/couch_eunit_proper.hrl"). +-else. +-include_lib("couch/include/couch_eunit.hrl"). +-endif. + +% Some bogus time in the far future to avoid reaching it and have that +% interfere with the tests +% +-define(TEST_TIME, "3000-01-01T00:00:00Z"). + +new_test() -> + New = couch_time_seq:new(), + ?assert(couch_time_seq:check(New)), + TSeq1 = couch_time_seq:update(New, 1), + ?assert(couch_time_seq:check(TSeq1)), + Upgraded = couch_time_seq:new(New), + ?assert(couch_time_seq:check(Upgraded)). + +check_test() -> + New = couch_time_seq:new(), + ?assert(couch_time_seq:check(New)), + ?assertNot(couch_time_seq:check(0)), + ?assertNot(couch_time_seq:check([])), + ?assertNot(couch_time_seq:check(#{})), + ?assertNot(couch_time_seq:check(#{v => 1, bins => junk})). + +min_time_limit_test() -> + TSeq = couch_time_seq:new(), + MinTime = couch_time_seq:update(TSeq, 1, 42), + ?assertEqual(TSeq, MinTime). + +update_first_test() -> + TSeq = couch_time_seq:new(), + TSeq1 = couch_time_seq:update(TSeq, test_time(), 1), + ?assertNotEqual(TSeq, TSeq1), + ?assertEqual(1, length(maps:get(bins, TSeq1))). + +update_same_hour_test() -> + TSeq = couch_time_seq:new(), + T = test_time(), + TSeq1 = couch_time_seq:update(TSeq, T, 1), + TSeq2 = couch_time_seq:update(TSeq1, T, 2), + #{bins := Bins} = TSeq2, + ?assertMatch([{_, 1}], Bins), + TSeq3 = couch_time_seq:update(TSeq2, T, 1), + ?assertEqual(TSeq2, TSeq3). + +stale_update_test() -> + TSeq = couch_time_seq:new(), + T = test_time(), + TSeq1 = couch_time_seq:update(TSeq, T + hours(3), 42), + TSeq2 = couch_time_seq:update(TSeq1, T, 43), + ?assertEqual(TSeq1, TSeq2). + +update_new_hour_test() -> + TSeq = couch_time_seq:new(), + TSeq1 = couch_time_seq:update(TSeq, test_time() + hours(3), 42), + TSeq2 = couch_time_seq:update(TSeq1, test_time() + hours(6), 43), + ?assertNotEqual(TSeq1, TSeq2), + #{bins := Bins} = TSeq2, + ?assertEqual(2, length(Bins)), + ?assertMatch([{_, 43}, {_, 42}], Bins). + +update_large_gap_test() -> + TSeq = couch_time_seq:new(), + T = test_time(), + TSeq1 = couch_time_seq:update(TSeq, T + hours(1), 42), + TSeq2 = couch_time_seq:update(TSeq1, T + years(30), 43), + ?assertNotEqual(TSeq1, TSeq2), + #{bins := Bins} = TSeq2, + ?assertEqual(2, length(Bins)), + ?assertMatch([{_, 43}, {_, 42}], Bins). + +update_when_bins_are_full_test() -> + TSeq = fill_bins(), + #{bins := Bins} = TSeq, + ?assertEqual(60, length(Bins)), + #{bins := [{TopTime, TopSeq} | _]} = TSeq, + TSeq1 = couch_time_seq:update(TSeq, TopTime + hours(1), TopSeq + 1), + #{bins := Bins1} = TSeq1, + ?assert(length(Bins1) =< 60), + TSeq2 = couch_time_seq:update(TSeq, TopTime + hours(2), TopSeq + 2), Review Comment: Good catch, it was supposed to be two sequential updates. I'll add one and add another update that's a much larger step (some years) and still assert that length stays fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@couchdb.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org