Home | History | Annotate | Download | only in engine
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef SYNC_TEST_ENGINE_MOCK_NON_BLOCKING_TYPE_PROCESSOR_H_
      6 #define SYNC_TEST_ENGINE_MOCK_NON_BLOCKING_TYPE_PROCESSOR_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/macros.h"
     12 #include "sync/engine/non_blocking_sync_common.h"
     13 #include "sync/engine/non_blocking_type_processor_interface.h"
     14 
     15 namespace syncer {
     16 
     17 // Mocks the NonBlockingTypeProcessor.
     18 //
     19 // This mock is made simpler by not using any threads.  It does still have the
     20 // ability to defer execution if we need to test race conditions, though.
     21 //
     22 // It maintains some state to try to make its behavior more realistic.  It
     23 // updates this state as it creates commit requests or receives update and
     24 // commit responses.
     25 //
     26 // It keeps a log of all received messages so tests can make assertions based
     27 // on their value.
     28 class MockNonBlockingTypeProcessor : public NonBlockingTypeProcessorInterface {
     29  public:
     30   MockNonBlockingTypeProcessor();
     31   virtual ~MockNonBlockingTypeProcessor();
     32 
     33   // Implementation of NonBlockingTypeProcessorInterface.
     34   virtual void ReceiveCommitResponse(
     35       const DataTypeState& type_state,
     36       const CommitResponseDataList& response_list) OVERRIDE;
     37   virtual void ReceiveUpdateResponse(
     38       const DataTypeState& type_state,
     39       const UpdateResponseDataList& response_list) OVERRIDE;
     40 
     41   // By default, this object behaves as if all messages are processed
     42   // immediately.  Sometimes it is useful to defer work until later, as might
     43   // happen in the real world if the model thread's task queue gets backed up.
     44   void SetSynchronousExecution(bool is_synchronous);
     45 
     46   // Runs any work that was deferred while this class was in asynchronous mode.
     47   //
     48   // This function is not useful unless this object is set to synchronous
     49   // execution mode, which is the default.
     50   void RunQueuedTasks();
     51 
     52   // Generate commit or deletion requests to be sent to the server.
     53   // These functions update local state to keep sequence numbers consistent.
     54   //
     55   // A real NonBlockingTypeProcessor would forward these kinds of messages
     56   // directly to its attached NonBlockingTypeProcessorCore.  These methods
     57   // return the value to the caller so the test framework can handle them as it
     58   // sees fit.
     59   CommitRequestData CommitRequest(const std::string& tag_hash,
     60                                   const sync_pb::EntitySpecifics& specifics);
     61   CommitRequestData DeleteRequest(const std::string& tag_hash);
     62 
     63   // Getters to access the log of received update responses.
     64   //
     65   // Does not includes repsonses that are in pending tasks.
     66   size_t GetNumUpdateResponses() const;
     67   UpdateResponseDataList GetNthUpdateResponse(size_t n) const;
     68   DataTypeState GetNthTypeStateReceivedInUpdateResponse(size_t n) const;
     69 
     70   // Getters to access the log of received commit responses.
     71   //
     72   // Does not includes repsonses that are in pending tasks.
     73   size_t GetNumCommitResponses() const;
     74   CommitResponseDataList GetNthCommitResponse(size_t n) const;
     75   DataTypeState GetNthTypeStateReceivedInCommitResponse(size_t n) const;
     76 
     77   // Getters to access the lastest update response for a given tag_hash.
     78   bool HasUpdateResponse(const std::string& tag_hash) const;
     79   UpdateResponseData GetUpdateResponse(const std::string& tag_hash) const;
     80 
     81   // Getters to access the lastest commit response for a given tag_hash.
     82   bool HasCommitResponse(const std::string& tag_hash) const;
     83   CommitResponseData GetCommitResponse(const std::string& tag_hash) const;
     84 
     85  private:
     86   // Process a received commit response.
     87   //
     88   // Implemented as an Impl method so we can defer its execution in some cases.
     89   void ReceiveCommitResponseImpl(const DataTypeState& type_state,
     90                                  const CommitResponseDataList& response_list);
     91 
     92   // Process a received update response.
     93   //
     94   // Implemented as an Impl method so we can defer its execution in some cases.
     95   void ReceiveUpdateResponseImpl(const DataTypeState& type_state,
     96                                  const UpdateResponseDataList& response_list);
     97 
     98   // Getter and setter for per-item sequence number tracking.
     99   int64 GetCurrentSequenceNumber(const std::string& tag_hash) const;
    100   int64 GetNextSequenceNumber(const std::string& tag_hash);
    101 
    102   // Getter and setter for per-item base version tracking.
    103   int64 GetBaseVersion(const std::string& tag_hash) const;
    104   void SetBaseVersion(const std::string& tag_hash, int64 version);
    105 
    106   // Getters and setter for server-assigned ID values.
    107   bool HasServerAssignedId(const std::string& tag_hash) const;
    108   const std::string& GetServerAssignedId(const std::string& tag_hash) const;
    109   void SetServerAssignedId(const std::string& tag_hash, const std::string& id);
    110 
    111   // State related to the implementation of deferred work.
    112   // See SetSynchronousExecution() for details.
    113   bool is_synchronous_;
    114   std::vector<base::Closure> pending_tasks_;
    115 
    116   // A log of messages received by this object.
    117   std::vector<CommitResponseDataList> received_commit_responses_;
    118   std::vector<UpdateResponseDataList> received_update_responses_;
    119   std::vector<DataTypeState> type_states_received_on_update_;
    120   std::vector<DataTypeState> type_states_received_on_commit_;
    121 
    122   // Latest responses received, indexed by tag_hash.
    123   std::map<const std::string, CommitResponseData> commit_response_items_;
    124   std::map<const std::string, UpdateResponseData> update_response_items_;
    125 
    126   // The per-item state maps.
    127   std::map<const std::string, int64> sequence_numbers_;
    128   std::map<const std::string, int64> base_versions_;
    129   std::map<const std::string, std::string> assigned_ids_;
    130 
    131   DISALLOW_COPY_AND_ASSIGN(MockNonBlockingTypeProcessor);
    132 };
    133 
    134 }  // namespace syncer
    135 
    136 #endif  // SYNC_TEST_ENGINE_MOCK_NON_BLOCKING_TYPE_PROCESSOR_H_
    137