anchor.diff:  -compile(export_all).
   
   % cmd(version, Sock, RecvCallback, Entry) ->
  -%     send_recv(Sock, RecvCallback, #mc_header{opcode = ?VERSION}, Entry);
  +%     send_recv(Sock, RecvCallback, #mc_header{opcode = ?VERSION}, Entry, <<"OK">>);
   
   cmd(get, Sock, RecvCallback, #mc_entry{keys = Keys}) ->
       ok = send(Sock,
  
positive.code:            #mc_entry{flag = Flag, expire = Expire} = Entry, Opcode) ->
      Ext = <<Flag:32, Expire:32>>,
      send_recv(Sock, RecvCallback,
                #mc_header{opcode = Opcode}, Entry#mc_entry{ext = Ext}).
  
  get_recv(Sock, RecvCallback) ->
      case recv(Sock, res) of
          {error, _} = Err -> Err;
          {ok, _Entry, #mc_header{opcode = ?NOOP}} ->
              {ok, <<"END">>};
          {ok, Entry, #mc_header{opcode = ?GETKQ} = Header} ->
              if is_function(RecvCallback) -> RecvCallback(Header, Entry);
                 true -> ok
              end,
              get_recv(Sock, RecvCallback)
      end.
  
  send_recv(Sock, RecvCallback, Header, Entry) ->
      ok = send(Sock, req, Header, Entry),
      {ok, RecvHeader, RecvEntry} = recv(Sock, res),
      if is_function(RecvCallback) -> RecvCallback(RecvHeader, RecvEntry);
         true -> ok
      end,
      {ok, RecvHeader, RecvEntry}.
  
  
negative.code:     {ok, Sock} = gen_tcp:connect("localhost", 11211,
                                   [binary, {packet, 0}, {active, false}]),
      (fun () ->
          {ok, works} = send_recv(Sock, nil,
                                  #mc_header{opcode = ?NOOP}, #mc_entry{},
                                  works)
      end)(),
  
      test_flush(Sock),
  
      ok = gen_tcp:close(Sock).
  
  test_flush(Sock) ->
      {ok, works} = send_recv(Sock, nil,
                              #mc_header{opcode = ?FLUSH}, #mc_entry{}, works).
  
      % (fun () ->
      %     {ok, RB1} = send_recv(Sock, <<"get ", Key/binary, "\r\n">>, nil),
      %     ?assertMatch(RB1, <<"END">>)
      % end)(),
  
  set_test() ->
      {ok, Sock} = gen_tcp:connect("localhost", 11211,
                                   [binary, {packet, 0}, {active, false}]),
      set_test_sock(Sock, <<"aaa">>),
      ok = gen_tcp:close(Sock).
  
  set_test_sock(Sock, Key) ->
      test_flush(Sock),
  
      (fun () ->
          {ok, RB} = cmd(set, Sock, nil,
                         #mc_entry{key = Key,
                                   data = <<"AAA">>}),
          ?assertMatch(RB, <<"STORED">>)
  
          % get_test_match(Sock, Key, <<"AAA">>)
      end)().
  