Exemple #1
0
void DateTimeTest::testArithmetics()
{
    DateTime dt1(2005, 1, 1, 0, 15, 30);
    DateTime dt2(2005, 1, 2, 0, 15, 30);
	
    Timespan s = dt2 - dt1;
    assert (s.days() == 1);
	
    DateTime dt3 = dt1 + s;
    assert (dt3 == dt2);
	
    dt3 -= s;
    assert (dt3 == dt1);
    dt1 += s;
    assert (dt1 == dt2);

    static const struct 
    {
        int lineNum;		// source line number
        int year1;			// operand/result date1 year
        int month1;			// operand/result date1 month
        unsigned int day1;	// operand/result date1 day
        int numDays;		// operand/result 'int' number of days
        int year2;			// operand/result date2 year
        int month2;			// operand/result date2 month
        unsigned int day2;	// operand/result date2 day
    } data[] = 
    {
        //          - - - -first- - - -           - - - second - - - 
        //line no.  year   month   day   numDays  year   month   day
        //-------   -----  -----  -----  -------  -----  -----  ----- 
        { __LINE__,       1,     1,     1,      1,      1,     1,     2 },
        { __LINE__,      10,     2,    28,      1,     10,     3,     1 },
        { __LINE__,     100,     3,    31,      2,    100,     4,     2 },
        { __LINE__,    1000,     4,    30,      4,   1000,     5,     4 },
        { __LINE__,    1000,     6,     1,    -31,   1000,     5,     1 },
        { __LINE__,    1001,     1,     1,   -365,   1000,     1,     1 },
        { __LINE__,    1100,     5,    31,     30,   1100,     6,    30 },
        { __LINE__,    1200,     6,    30,     32,   1200,     8,     1 },
        { __LINE__,    1996,     2,    28,    367,   1997,     3,     1 },
        { __LINE__,    1997,     2,    28,    366,   1998,     3,     1 },
        { __LINE__,    1998,     2,    28,    365,   1999,     2,    28 },
        { __LINE__,    1999,     2,    28,    364,   2000,     2,    27 },
        { __LINE__,    1999,     2,    28,   1096,   2002,     2,    28 },
        { __LINE__,    2002,     2,    28,  -1096,   1999,     2,    28 },
    };

    const int num_data = sizeof data / sizeof *data;
    for (int di = 0; di < num_data; ++di) 
    {
        const int line     = data[di].lineNum;
        const int num_days = data[di].numDays;
        DateTime x = DateTime(data[di].year1, data[di].month1, data[di].day1);
        const DateTime& X = x;
        x += Timespan(num_days, 0, 0, 0, 0);
        loop_1_assert(line, data[di].year2 == X.year());
        loop_1_assert(line, data[di].month2 == X.month());
        loop_1_assert(line, data[di].day2 == X.day());
    }
}
Exemple #2
0
void DateTimeTest::testUsage()
{
    DateTime dt1(1776, 7, 4);
    assert (dt1.year() == 1776);
    assert (dt1.month() == 7);
    assert (dt1.day() == 4);

    DateTime dt2(dt1);
    dt2 += Timespan(6, 0, 0, 0, 0);
    assert (dt2.year() == 1776);
    assert (dt2.month() == 7);
    assert (dt2.day() == 10);

    Timespan span = dt2 - dt1;
    assert (span.days() == 6);

    // TODO - When adding months and years we need to be
    // able to specify the end-end convention.
    // We cannot do this in POCO at the moment.
}
void KeyValueStore::InternalThread::outgoingRoutine()
{
  unsigned currId = -1;
  const Config::ServerInformation& info = config.getServerInformation();
  Config::ThreadControl& control = config.getThreadControl();
  Mutex& outMutex = control.getOutMutex();
  Condition& cond = control.getOutCondition();
  Timespan timeout(3, 0); // 3 seconds to receive response
  bool connected = false;
  unsigned contentLength = 0;
  char* buf = NULL;

  while(control.isLive()) {
    outMutex.lock();
    cond.wait(outMutex);

    // Check if server changed
    if(control.getReconnectedTo()) {
      printKv("Reconnecting");
      currId = control.getConnectedToId();
      const Config::ServerInformation& nextInfo = config.getServerInformation(currId);
      try {
        if(connected)
          sock.close();
        sock.connect(SocketAddress(nextInfo.address, nextInfo.internalPort), Timespan(5, 0));
        connected = true;
        printKv("Connected to outgoing server: "<< nextInfo.id);
        //sock.setReceiveTimeout(timeout);
        KeyValueStore::replicateDB(sock, control.getMachineCount());
      } catch(Exception& e) {
        printKv("Could not connect to next key-value internal server ("<< e.displayText() <<")" << endl
          << "System may be inconsistent until next reconnect.");
        connected = false;
      }
    }

    while(!forwardQueue.isEmpty()) {
      pair<char, string> req(forwardQueue.dequeue());
      if(req.first == Protocol::REPL_INS || req.first == Protocol::REPL_REM) {
        try {
          KeyValueStore::sendRequest(sock, req);
        } catch(Exception& e) {
          printKv("Could not replicate: "<< e.displayText());
        }
      } else if(req.first == Protocol::INS_MOVIE) { // Requests without responses
        KeyValueStore::sendRequest(sock, req); // Not waiting for a response
      } else if(req.first == Protocol::LUP_MOVIE && false) { // This returns many responses need to get all
        printKv("Received movie lookup forward");
        KeyValueStore::sendRequest(sock, req);
        
        pair<char, string> resp(KeyValueStore::rcvResponse(sock, buf));
        while(resp.first != Protocol::MLOOKUP_DONE && resp.first != Protocol::LOOKUP_NO) {
          resp = KeyValueStore::rcvResponse(sock, buf);
          respBuffer.enqueue(resp);
//          replyForward(); // Send as we get them
        }
        respBuffer.enqueue(resp); // Make sure we also send our done response

        printKv("Served movies");

        replyForward();
      } else {
        // Forward and wait for response.
        try {
          KeyValueStore::sendRequest(sock, req);

          pair<char, string> resp(KeyValueStore::rcvResponse(sock, buf));
          respBuffer.enqueue(resp);

          replyForward();
        } catch(Exception& e) {
          // If we timeout or anything, treat request as failed
          printKv("Exception while forwarding: "<< e.displayText());
          respBuffer.enqueue(pair<char, string>(Protocol::REQ_FAIL, ""));
          replyForward();
        }
      }
    }
 //   if(!respBuffer.isEmpty())
   //   replyForward();

    outMutex.unlock();
  }
}
Exemple #4
0
void DateTimeTest::testIncrementDecrement()
{
    static const struct 
    {
        int lineNum;		// source line number
        int year1;			// (first) date year
        int month1;			// (first) date month
        unsigned int day1;	// (first) date day
        int year2;			// (second) date year
        int month2;			// (second) date month
        unsigned int day2;	// (second) date day
    } data[] = 
    {
       //          - - - -first- - - -    - - - second - - - 
       //line no.  year   month   day     year   month   day
       //-------   -----  -----  -----    -----  -----  ----- 
      { __LINE__,       1,     1,     1,       1,     1,     2 },
      { __LINE__,      10,     2,    28,      10,     3,     1 },
      { __LINE__,     100,     3,    31,     100,     4,     1 },
      { __LINE__,    1000,     4,    30,    1000,     5,     1 },
      { __LINE__,    1100,     5,    31,    1100,     6,     1 },
      { __LINE__,    1200,     6,    30,    1200,     7,     1 },
      { __LINE__,    1300,     7,    31,    1300,     8,     1 },
      { __LINE__,    1400,     8,    31,    1400,     9,     1 },
      { __LINE__,    1500,     9,    30,    1500,    10,     1 },
      { __LINE__,    1600,    10,    31,    1600,    11,     1 },
      { __LINE__,    1700,    11,    30,    1700,    12,     1 },
      { __LINE__,    1800,    12,    31,    1801,     1,     1 },
      { __LINE__,    1996,     2,    28,    1996,     2,    29 },
      { __LINE__,    1997,     2,    28,    1997,     3,     1 },
      { __LINE__,    1998,     2,    28,    1998,     3,     1 },
      { __LINE__,    1999,     2,    28,    1999,     3,     1 },
      { __LINE__,    2000,     2,    28,    2000,     2,    29 },
      { __LINE__,    2001,     2,    28,    2001,     3,     1 },
      { __LINE__,    2004,     2,    28,    2004,     2,    29 },
      { __LINE__,    2100,     2,    28,    2100,     3,     1 },
      { __LINE__,    2400,     2,    28,    2400,     2,    29 },
    };

    const int num_data = sizeof data / sizeof *data;
    int di;

    for (di = 0; di < num_data; ++di) 
    {
        const int line = data[di].lineNum;
        DateTime x = DateTime(data[di].year1, data[di].month1, 
                              data[di].day1);
        // Would do pre-increment of x here.
        const DateTime& X = x;
        x = x + Timespan(1,0,0,0,0);  
        DateTime y = x; const DateTime& Y = y;

        loop_1_assert(line, data[di].year2  == X.year());
        loop_1_assert(line, data[di].month2 == X.month());
        loop_1_assert(line, data[di].day2   == X.day());

        loop_1_assert(line, data[di].year2  == Y.year());
        loop_1_assert(line, data[di].month2 == Y.month());
        loop_1_assert(line, data[di].day2   == Y.day());
    }

    for (di = 0; di < num_data; ++di) 
    {
        const int line = data[di].lineNum;
        DateTime x = DateTime(data[di].year1, data[di].month1, data[di].day1);
        DateTime x1 = DateTime(data[di].year1, data[di].month1, data[di].day1);
        DateTime x2 = DateTime(data[di].year2, data[di].month2, data[di].day2);
        DateTime y = x; const DateTime& Y = y;

        // Would do post increment of x here.
        const DateTime& X = x;
        x = x + Timespan(1,0,0,0,0);  

        loop_1_assert(line, data[di].year2  == X.year());
        loop_1_assert(line, data[di].month2 == X.month());
        loop_1_assert(line, data[di].day2   == X.day());
        loop_1_assert(line, data[di].year1  == Y.year());
        loop_1_assert(line, data[di].month1 == Y.month());
        loop_1_assert(line, data[di].day1   == Y.day());
    }
            
    for (di = 0; di < num_data; ++di) 
    {
        const int line = data[di].lineNum;
        DateTime x = DateTime(data[di].year2, data[di].month2, data[di].day2);
        const DateTime& X = x;
        x = x - Timespan(1,0,0,0,0);  
        DateTime y = x; DateTime Y = y;

        loop_1_assert(line, data[di].year1  == X.year());
        loop_1_assert(line, data[di].month1 == X.month());
        loop_1_assert(line, data[di].day1   == X.day());

        loop_1_assert(line, data[di].year1  == Y.year());
        loop_1_assert(line, data[di].month1 == Y.month());
        loop_1_assert(line, data[di].day1   == Y.day());
    }

    for (di = 0; di < num_data; ++di) 
    {
        const int line = data[di].lineNum;
        DateTime x1 = DateTime(data[di].year1, data[di].month1, data[di].day1);
        DateTime x = DateTime(data[di].year2, data[di].month2, data[di].day2);
        DateTime y = x; DateTime Y = y;
        const DateTime& X = x;
        // would post-decrement x here.
        x = x - Timespan(1,0,0,0,0);  

        loop_1_assert(line, data[di].year1  == X.year());
        loop_1_assert(line, data[di].month1 == X.month());
        loop_1_assert(line, data[di].day1   == X.day());

        loop_1_assert(line, data[di].year2  == Y.year());
        loop_1_assert(line, data[di].month2 == Y.month());
        loop_1_assert(line, data[di].day2   == Y.day());
    }
}