Changeset d85532


Ignore:
Timestamp:
Dec 13, 2011, 12:00:30 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Children:
b760ac5
Parents:
2c11c1
git-author:
Frederik Heber <heber@…> (12/02/11 10:10:17)
git-committer:
Frederik Heber <heber@…> (12/13/11 12:00:30)
Message:

Added unit test on Relay.

  • added new stubs to ObserverStub implementing Relay.
  • Relay is friend of Notification.
Location:
src
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • src/Makefile.am

    r2c11c1 rd85532  
    4141        Patterns/Observer/Notification.cpp \
    4242        Patterns/Observer/Observable.cpp \
    43         Patterns/Observer/Observer.cpp
     43        Patterns/Observer/Observer.cpp \
     44        Patterns/Observer/Relay.cpp
    4445PATTERNSOURCEDEBUG = \
    4546        $(PATTERNSOURCE) \
     
    6465        Patterns/Observer/ObservedIterator.hpp \
    6566        Patterns/Observer/ObservedContainer_impl.hpp \
     67        Patterns/Observer/Relay.hpp \
    6668        Patterns/Registry_impl.hpp \
    6769        Patterns/Singleton_impl.hpp \
  • src/Patterns/Observer/Notification.hpp

    r2c11c1 rd85532  
    1616#include <set>
    1717
    18 class Channels;
    1918class Observable;
    2019class Observer;
     20class Relay;
    2121
    2222/** Notifications are sort of news channels of an Observable.
     
    2828 */
    2929class Notification {
     30  //!> grant Observable access to notifyAll()
    3031  friend class Observable;
    31   friend class Channels;
     32  //!> grant Relay access to notifyAll()
     33  friend class Relay;
    3234public:
    3335  Notification(size_t _channelno);
  • src/Patterns/Observer/Observable.hpp

    r2c11c1 rd85532  
    114114  static PriorityLevel PriorityDefault;
    115115
    116 private:
     116protected:
    117117  typedef std::multimap<int,Observer*> callees_t;
    118118  typedef std::set<Notification*> notificationSet;
  • src/Patterns/Observer/Observer.hpp

    r2c11c1 rd85532  
    4444class Observer
    4545{
     46  friend class Notification;
    4647  friend class Observable;
    47   friend class Notification;
     48  friend class Relay;
    4849  template<class> friend class ObservedIterator;
    4950
  • src/Patterns/Observer/Relay.cpp

    r2c11c1 rd85532  
    2222#include "Relay.hpp"
    2323
     24#include "Assert.hpp"
     25#include "Channels.hpp"
     26#include "defs.hpp"
     27#include "Notification.hpp"
     28
    2429
    2530/** Constructor for class Relay.
    2631 */
    27 Relay::Relay(string name) :
    28   Observer(Observer::BaseConstructor())
     32Relay::Relay(std::string name) :
     33  Observable(name),
     34  Updater(NULL)
    2935{
    3036#ifdef LOG_OBSERVER
     
    4248  observerLog().addMessage() << "-- Destroying Relay " << observerLog().getName(this);
    4349#endif
    44   if(callTable.count(this)) {
    45     // delete all entries for this observable
    46     callees_t callees = callTable[this];
    47     callees_t::iterator iter;
    48     for(iter=callees.begin();iter!=callees.end();++iter){
    49       (*iter).second->subjectKilled(this);
    50     }
    51     callTable.erase(this);
    52   }
    5350}
    5451
    5552
    5653/** Notify all Observers of changes.
    57  * Puts \a *this into Relay::busyRelays, calls Observer::update() for all in callee_t
     54 * Puts \a *this into Relay::busyObservables, calls Observer::update() for all in callee_t
    5855 * and removes from busy list.
    5956 */
    6057void Relay::notifyAll() {
     58  ASSERT(Updater != NULL,
     59      "Relay::notifyAll() called while Updater is NULL.");
    6160  // we are busy notifying others right now
    6261  // add ourselves to the list of busy subjects to enable circle detection
    63   busyRelays.insert(this);
     62  busyObservables.insert(this);
    6463  // see if anyone has signed up for observation
    6564  // and call all observers
     
    8685  // send out all notifications that need to be done
    8786
    88   notificationSet currentNotifications = notifications[this];
     87  notificationSet currentNotifications = notifications[Updater];
    8988  for(notificationSet::iterator it = currentNotifications.begin();
    9089      it != currentNotifications.end();++it){
    91     (*it)->notifyAll();
     90    (*it)->notifyAll(Updater);
    9291  }
    9392
    94   notifications.erase(this);
     93  notifications.erase(Updater);
    9594
    9695   // done with notification, we can leave the set of busy subjects
    97   busyRelays.erase(this);
     96  busyObservables.erase(this);
    9897}
    9998
     
    106105void Relay::update(Observable *publisher) {
    107106  // circle detection
    108   if(busyRelays.find(this)!=busyRelays.end()) {
     107  if(busyObservables.find(this)!=busyObservables.end()) {
    109108    // somehow a circle was introduced... we were busy notifying our
    110109    // observers, but still we are called by one of our sub-Relays
     
    122121#ifdef LOG_OBSERVER
    123122      observerLog().addMessage() << "-* Update from " << observerLog().getName(publisher)
    124                                  << " relayed by " << observerLog().getName(this) << endl;
     123                                 << " relayed by " << observerLog().getName(this);
    125124#endif
    126125      Updater = publisher;
     
    130129#ifdef LOG_OBSERVER
    131130      observerLog().addMessage() << "-| Update from " <<  observerLog().getName(publisher)
    132                                  << " not relayed by " << observerLog().getName(this) << endl;
     131                                 << " not relayed by " << observerLog().getName(this);
    133132#endif
    134133    }
  • src/Patterns/Observer/Relay.hpp

    r2c11c1 rd85532  
    1717
    1818/**
    19  * An Relay acts as a node in a many-to-one observation.
     19 * A Relay acts as a node in a many-to-one observation.
    2020 *
    2121 * All Observable::update() calls will pass through this relay. Hence, if an
  • src/Patterns/Observer/unittests/ObserverUnitTest.cpp

    r2c11c1 rd85532  
    6868  notificationObserver2 = new NotificationObserver(
    6969      notificationObservable->getChannel(NotificationObservable::Operation2Notify));
     70
     71  RelayObservable = new RelayTest;
     72  RelayObserver = new RelayCountObserver(RelayObservable);
    7073}
    7174
     
    7982  delete obsset;
    8083  delete obsmap;
     84  delete RelayObservable;
     85  delete RelayObserver;
    8186
    8287  delete observer1;
     
    237242}
    238243
     244void ObserverTest::relayTest()
     245{
     246  // sign on some observables to the relay
     247  simpleObservable1->signOn(RelayObservable);
     248  simpleObservable2->signOn(RelayObservable);
     249
     250  // sign on an observer to the relay
     251  RelayObservable->signOn(RelayObserver);
     252  simpleObservable1->signOn(observer1);
     253
     254  // check that all is zero
     255  CPPUNIT_ASSERT_EQUAL( 0, RelayObserver->updates );
     256  CPPUNIT_ASSERT_EQUAL( 0, observer1->updates );
     257
     258  // signal update
     259  simpleObservable1->changeMethod();
     260
     261  // check that both the change
     262  // (and RelayObserver checks being called correctly)
     263  CPPUNIT_ASSERT_EQUAL( 1, RelayObserver->updates );
     264  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
     265
     266  // signal update through relay only
     267  simpleObservable2->changeMethod();
     268
     269  // check that only one got the change
     270  CPPUNIT_ASSERT_EQUAL( 2, RelayObserver->updates );
     271  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
     272
     273  // check on signOff
     274  simpleObservable1->signOff(RelayObservable);
     275  simpleObservable1->signOff(observer1);
     276}
     277
    239278void ObserverTest::CircleDetectionTest() {
    240279  std::cout << std::endl << "Warning: the next test involved methods that can produce infinite loops." << std::endl;
  • src/Patterns/Observer/unittests/ObserverUnitTest.hpp

    r2c11c1 rd85532  
    2424class ObservableSet;
    2525class ObservableMap;
     26class RelayObservable;
    2627class BlockObservable;
    2728class NotificationObservable;
     29class RelayCountObserver;
    2830
    2931class ObserverTest :  public CppUnit::TestFixture
     
    3739  CPPUNIT_TEST ( doesReportTest );
    3840  CPPUNIT_TEST ( iteratorTest );
     41  CPPUNIT_TEST ( relayTest );
    3942  CPPUNIT_TEST ( CircleDetectionTest );
    4043  CPPUNIT_TEST_SUITE_END();
     
    5154  void doesReportTest();
    5255  void iteratorTest();
     56  void relayTest();
    5357  void CircleDetectionTest();
    5458
     
    7074  ObservableSet *obsset;
    7175  ObservableMap *obsmap;
    72 
     76  RelayTest *RelayObservable;
     77  RelayCountObserver *RelayObserver;
    7378};
    7479
  • src/Patterns/Observer/unittests/stubs/ObserverStub.cpp

    r2c11c1 rd85532  
    230230}
    231231
     232/****************** Relay *********************/
     233
     234RelayTest::RelayTest() :
     235    Relay(std::string("RelayTest"))
     236{}
     237
     238RelayTest::~RelayTest()
     239{}
     240
     241/************ RelayCountObserver ***************/
     242
     243RelayCountObserver::RelayCountObserver(const Observable * const _relay) :
     244    Observer("RelayCountObserver"),
     245    updates(0),
     246    relay(_relay)
     247{}
     248
     249RelayCountObserver::~RelayCountObserver()
     250{}
     251
     252void RelayCountObserver::update(Observable *publisher){
     253  // check that we are not called by the relay itself
     254  CPPUNIT_ASSERT(publisher != relay);
     255  updates++;
     256}
     257
     258void RelayCountObserver::subjectKilled(Observable *publisher) {
     259}
     260
  • src/Patterns/Observer/unittests/stubs/ObserverStub.hpp

    r2c11c1 rd85532  
    1212#include "Observer/Observable.hpp"
    1313#include "Observer/ObservedIterator.hpp"
     14#include "Observer/Relay.hpp"
    1415
    1516class UpdateCountObserver : public Observer {
     
    126127};
    127128
     129class RelayTest : public Relay
     130{
     131public:
     132  RelayTest();
     133  ~RelayTest();
     134private:
     135};
     136
     137class RelayCountObserver : public Observer {
     138public:
     139  RelayCountObserver(const Observable * const relay);
     140  virtual ~RelayCountObserver();
     141
     142  void update(Observable *publisher);
     143  void subjectKilled(Observable *publisher);
     144
     145  int updates;
     146private:
     147  const Observable * const relay;
     148};
    128149
    129150#endif /* OBSERVERSTUB_HPP_ */
Note: See TracChangeset for help on using the changeset viewer.