diff --git a/agent/agent.cpp b/agent/agent.cpp index b10912a05..5369553fa 100644 --- a/agent/agent.cpp +++ b/agent/agent.cpp @@ -1257,15 +1257,9 @@ string Agent::fetchSampleData(std::set &aFilter, // START SHOULD BE BETWEEN 0 AND SEQUENCE NUMBER start = (start <= firstSeq) ? firstSeq : start; - end = start + count; - if (end >= mSequence) { - end = mSequence; - endOfBuffer = true; - } else { - endOfBuffer = false; - } - for (uint64_t i = start; i < end; i++) + uint64_t i; + for (i = start; results.size() < count && i < mSequence; i++) { // Filter out according to if it exists in the list const string &dataId = (*mSlidingBuffer)[i]->getDataItem()->getId(); @@ -1276,6 +1270,12 @@ string Agent::fetchSampleData(std::set &aFilter, } } + end = i; + if (i >= mSequence) + endOfBuffer = true; + else + endOfBuffer = false; + if (aObserver != NULL) aObserver->reset(); } diff --git a/test/agent_test.cpp b/test/agent_test.cpp index 64d012c5d..3ee881919 100644 --- a/test/agent_test.cpp +++ b/test/agent_test.cpp @@ -581,6 +581,46 @@ void AgentTest::testSequenceNumberRollover() #endif } +void AgentTest::testSampleCount() +{ + key_value_map kvm; + + adapter = a->addAdapter("LinuxCNC", "server", 7878, false); + CPPUNIT_ASSERT(adapter); + + int64_t seq = a->getSequence(); + + // Get the current position + char line[80]; + + // Add many events + for (int i = 0; i < 128; i++) + { + sprintf(line, "TIME|line|%d|Xact|%d", i, i); + adapter->processData(line); + } + + { + path = "/sample"; + kvm["path"] = "//DataItem[@name='Xact']"; + kvm["from"] = int64ToString(seq); + kvm["count"] = "10"; + + PARSE_XML_RESPONSE_QUERY(kvm); + CPPUNITTEST_ASSERT_XML_PATH_EQUAL(doc, "//m:Header@nextSequence", + int64ToString(seq + 20).c_str()); + + CPPUNITTEST_ASSERT_XML_PATH_COUNT(doc, "//m:DeviceStream//m:Position", 10); + + // Make sure we got 10 lines + for (int j = 0; j < 10; j++) + { + sprintf(line, "//m:DeviceStream//m:Position[%d]@sequence", j + 1); + CPPUNITTEST_ASSERT_XML_PATH_EQUAL(doc, line, int64ToString(seq + j * 2 + 1).c_str()); + } + } + +} void AgentTest::testAdapterCommands() { diff --git a/test/agent_test.hpp b/test/agent_test.hpp index 45c7b8729..c9f44643e 100644 --- a/test/agent_test.hpp +++ b/test/agent_test.hpp @@ -91,6 +91,7 @@ class AgentTest : public CppUnit::TestFixture CPPUNIT_TEST(testAssetStorageWithoutType); CPPUNIT_TEST(testStreamData); CPPUNIT_TEST(testSequenceNumberRollover); + CPPUNIT_TEST(testSampleCount); CPPUNIT_TEST(testStreamDataObserver); CPPUNIT_TEST(testFailWithDuplicateDeviceUUID); CPPUNIT_TEST(testMultipleDisconnect); @@ -199,6 +200,7 @@ class AgentTest : public CppUnit::TestFixture // Sequence number tests void testSequenceNumberRollover(); + void testSampleCount(); // Test failure when adding a duplicate device uuid. void testFailWithDuplicateDeviceUUID();