Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 143 lines (128 sloc) 5.952 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
-module(luwak_get_stream_tests).

-ifdef(TEST).

-include_lib("eunit/include/eunit.hrl").

get_stream_test_() ->
    {spawn,
     [{setup,
       fun test_helper:setup/0,
       fun test_helper:cleanup/1,
       [
        {timeout, 60000,
         [fun simple_get_stream/0,
          fun three_level_tree_stream/0,
          fun read_beyond_file_end/0,
          fun partial_end_block_get_range/0,
          fun partial_start_block_get_range/0,
          fun partial_middle_block_get_range/0]}
       ]
      }
     ]
    }.

simple_get_stream() ->
  test_helper:riak_test(fun(Riak) ->
      {ok, File} = luwak_file:create(Riak, <<"file1">>, [{block_size,3},{tree_order,3}], dict:new()),
      {ok, _Written, File1} = luwak_io:put_range(Riak, File, 0, <<"abcdefghijklmnopqrstuvwxyz">>),
      GetStream = luwak_get_stream:start(Riak, File1, 0, 26),
      timer:sleep(100),
      ?assertEqual({<<"abc">>, 0}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"def">>, 3}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"ghi">>, 6}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"jkl">>, 9}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"mno">>, 12}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"pqr">>, 15}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"stu">>, 18}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"vwx">>, 21}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual({<<"yz">>, 24}, luwak_get_stream:recv(GetStream, 1000)),
      ?assertEqual(eos, luwak_get_stream:recv(GetStream, 1000))
    end).

three_level_tree_stream() ->
  Test = fun() ->
    test_helper:riak_test(fun(Riak) ->
        {ok, File} = luwak_file:create(Riak, <<"file1">>, [{block_size,2},{tree_order,2}], dict:new()),
        {ok, _Written, File1} = luwak_io:put_range(Riak, File, 0, <<"abcdefghijklmnopqrstuvwxyz">>),
        GetStream = luwak_get_stream:start(Riak, File1, 3, 10),
        ?assertEqual({<<"d">>, 3}, luwak_get_stream:recv(GetStream, 1000)),
        ?assertEqual({<<"ef">>, 4}, luwak_get_stream:recv(GetStream, 1000)),
        ?assertEqual({<<"gh">>, 6}, luwak_get_stream:recv(GetStream, 1000)),
        ?assertEqual({<<"ij">>, 8}, luwak_get_stream:recv(GetStream, 1000)),
        ?assertEqual({<<"kl">>, 10}, luwak_get_stream:recv(GetStream, 1000)),
        ?assertEqual({<<"m">>, 12}, luwak_get_stream:recv(GetStream, 1000)),
        ?assertEqual(eos, luwak_get_stream:recv(GetStream, 1000))
      end)
    end,
  {timeout, 30000, Test}.

read_beyond_file_end() ->
  test_helper:riak_test(fun(Riak) ->
      {ok, File} = luwak_file:create(Riak, <<"file1">>, [{block_size,2},{tree_order,2}], dict:new()),
      {ok, _Written, File1} = luwak_io:put_range(Riak, File, 0, <<"abscdefghijklmnopqrstuvwxyz">>),
      GetStream = luwak_get_stream:start(Riak, File1, 30, 10),
      ?assertEqual(eos, luwak_get_stream:recv(GetStream, 1000))
    end).

%% @doc Makes a simple file, named Name, with BlockCount blocks, each
%% filled with BlockSize characters.
%% Ex: make_standard_range_file(R, <<"foo">>, 5, 3)
%% will make a file named "foo" with the contents:
%% aaaaabbbbbccccc
%% @spec make_standard_range_file(riak_client(), binary(),
%% integer(), integer())
%% -> {ok, Contents::binary(), FileHandle::luwak_file()}
make_standard_range_file(Riak, Name, BlockSize, BlockCount) ->
    {ok, File} = luwak_file:create(Riak, Name,
                                   [{block_size, BlockSize}],
                                   dict:new()),
    Data = iolist_to_binary(
             [ lists:duplicate(BlockSize, $a+N) ||
                 N <- lists:seq(0, BlockCount-1) ]),
    {ok, _, NewFile} = luwak_io:put_range(Riak, File, 0, Data),
    {ok, Data, NewFile}.

%% tests a get_range that specifies an offset+length that ends in
%% the middle of a block
%% Ex: aaaaaaaaaa bbbbbbbbbb cccccccccc
%% ^start end^
partial_end_block_get_range() ->
    test_helper:riak_test(
      fun(Riak) ->
              {ok, Data, File} = make_standard_range_file(
                                    Riak, <<"endblockrange">>, 10, 3),
              Read = read_stream(
                       luwak_get_stream:start(Riak, File, 10, 14)),
              <<_:10/binary, Expected:14/binary, _/binary>> = Data,
              ?assertEqual(Expected, Read)
      end).

%% tests a get_range that specifies an offset that lands in the
%% middle of a block
%% Ex: aaaaaaaaaa bbbbbbbbbb cccccccccc
%% ^start end^
partial_start_block_get_range() ->
    test_helper:riak_test(
      fun(Riak) ->
              {ok, Data, File} = make_standard_range_file(
                                   Riak, <<"startblockrange">>, 10, 3),
              Read = read_stream(
                       luwak_get_stream:start(Riak, File, 5, 15)),
              <<_:5/binary, Expected:15/binary, _/binary>> = Data,
              ?assertEqual(Expected, Read)
      end).


%% tests a get_range that starts and ends in the same block, but
%% is unaligned with that block
%% Ex: aaaaaaaaaa bbbbbbbbbb cccccccccc
%% ^st end^
partial_middle_block_get_range() ->
    test_helper:riak_test(
      fun(Riak) ->
              {ok, Data, File} = make_standard_range_file(
                                   Riak, <<"midblockrange">>, 10, 3),
               Read = read_stream(
                        luwak_get_stream:start(Riak, File, 11, 8)),
               <<_:11/binary, Expected:8/binary, _/binary>> = Data,
              ?assertEqual(Expected, Read)
      end).

read_stream(Stream) ->
    read_stream(Stream, luwak_get_stream:recv(Stream, 1000), []).

read_stream(_, eos, Acc) ->
    iolist_to_binary(lists:reverse(Acc));
read_stream(Stream, {Data, _}, Acc) ->
    read_stream(Stream,
                luwak_get_stream:recv(Stream, 1000),
                [Data|Acc]).

-endif.
Something went wrong with that request. Please try again.