From 3d402962aa2f2d3a194d32bad813fdbd6e96a502 Mon Sep 17 00:00:00 2001 From: maroy Date: Fri, 8 Apr 2005 16:53:29 +0000 Subject: [PATCH] now playlists scheduled too late (after init but before start) will not get executed. solution for bug http://bugs.campware.org/view.php?id=757 --- .../eventScheduler/src/EventSchedulerTest.cxx | 55 ++++++++++++++++++- .../eventScheduler/src/EventSchedulerTest.h | 14 ++++- .../eventScheduler/src/TestEventContainer.cxx | 4 +- .../scheduler/bin/scheduler_devenv.sh | 10 +++- .../products/scheduler/etc/Makefile.in | 11 ++-- .../products/scheduler/src/PlaylistEvent.cxx | 28 +++++++++- .../products/scheduler/src/PlaylistEvent.h | 28 ++++++++-- .../scheduler/src/RpcUploadPlaylistTest.cxx | 44 ++++++++++++++- .../scheduler/src/RpcUploadPlaylistTest.h | 16 +++++- 9 files changed, 189 insertions(+), 21 deletions(-) diff --git a/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx b/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx index 16d2e4ea5..a91600c5d 100644 --- a/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx +++ b/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/EventSchedulerTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -181,3 +181,56 @@ EventSchedulerTest :: firstTest(void) eventScheduler->stop(); } + +/*------------------------------------------------------------------------------ + * A test to see what happens, when an event is scheduled after it should + * have been initialized, but not to be started yet. + * See http://bugs.campware.org/view.php?id=757 for details. + *----------------------------------------------------------------------------*/ +void +EventSchedulerTest :: postInitTest(void) + throw (CPPUNIT_NS::Exception) +{ + Ptr::Ref event; + Ptr::Ref container; + Ptr::Ref eventScheduler; + Ptr::Ref now; + Ptr::Ref when; + Ptr::Ref initTime; + Ptr::Ref eventLength; + Ptr::Ref granularity; + TestScheduledEvent::State state; + + /* time timeline for this test is: + initialize - 10 sec + start - now + 5sec + stop - start + 3 sec + */ + + now = TimeConversion::now(); + when.reset(new ptime(*now + seconds(5))); + initTime.reset(new time_duration(seconds(10))); + eventLength.reset(new time_duration(seconds(3))); + granularity.reset(new time_duration(seconds(1))); + + event.reset(new TestScheduledEvent(when, initTime, eventLength)); + container.reset(new TestEventContainer(event)); + + eventScheduler.reset(new EventScheduler(container, granularity)); + + eventScheduler->start(); + + CPPUNIT_ASSERT(event->getState() == TestScheduledEvent::created); + state = event->getState(); + + Ptr::Ref end(new ptime(*when + seconds(10))); + while (*TimeConversion::now() < *end) { + // nothing should happen here, just wait for some to see that + // indeed it doesn't + CPPUNIT_ASSERT(event->getState() == TestScheduledEvent::created); + TimeConversion::sleep(granularity); + } + + eventScheduler->stop(); +} + diff --git a/livesupport/modules/eventScheduler/src/EventSchedulerTest.h b/livesupport/modules/eventScheduler/src/EventSchedulerTest.h index 439913ea4..e0f3c754c 100644 --- a/livesupport/modules/eventScheduler/src/EventSchedulerTest.h +++ b/livesupport/modules/eventScheduler/src/EventSchedulerTest.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/EventSchedulerTest.h,v $ ------------------------------------------------------------------------------*/ @@ -58,13 +58,14 @@ namespace EventScheduler { * Unit test for the EventScheduler class. * * @author $Author: maroy $ - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * @see EventScheduler */ class EventSchedulerTest : public CPPUNIT_NS::TestFixture { CPPUNIT_TEST_SUITE(EventSchedulerTest); CPPUNIT_TEST(firstTest); + CPPUNIT_TEST(postInitTest); CPPUNIT_TEST_SUITE_END(); private: @@ -80,6 +81,15 @@ class EventSchedulerTest : public CPPUNIT_NS::TestFixture void firstTest(void) throw (CPPUNIT_NS::Exception); + /** + * A test to see if an event is scheduled after it should have + * been initialized (see http://bugs.campware.org/view.php?id=757) + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + postInitTest(void) throw (CPPUNIT_NS::Exception); + public: /** diff --git a/livesupport/modules/eventScheduler/src/TestEventContainer.cxx b/livesupport/modules/eventScheduler/src/TestEventContainer.cxx index 89f5725d8..aa970e085 100644 --- a/livesupport/modules/eventScheduler/src/TestEventContainer.cxx +++ b/livesupport/modules/eventScheduler/src/TestEventContainer.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.1 $ + Version : $Revision: 1.2 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/modules/eventScheduler/src/TestEventContainer.cxx,v $ ------------------------------------------------------------------------------*/ @@ -74,7 +74,7 @@ TestEventContainer :: TestEventContainer( Ptr::Ref TestEventContainer :: getNextEvent(Ptr::Ref when) throw () { - if (*when < *event->getScheduledTime()) { + if (*when < (*event->getScheduledTime() - *event->maxTimeToInitialize())) { return event; } else { // return an empty reference diff --git a/livesupport/products/scheduler/bin/scheduler_devenv.sh b/livesupport/products/scheduler/bin/scheduler_devenv.sh index c75270467..f31b99442 100755 --- a/livesupport/products/scheduler/bin/scheduler_devenv.sh +++ b/livesupport/products/scheduler/bin/scheduler_devenv.sh @@ -21,7 +21,7 @@ # # # Author : $Author: maroy $ -# Version : $Revision: 1.1 $ +# Version : $Revision: 1.2 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/bin/scheduler_devenv.sh,v $ #------------------------------------------------------------------------------- @@ -81,6 +81,12 @@ case "$mode" in sleep 2 ;; + 'run') + echo "Running the LiveSupport scheduler..." + $scheduler_exe -c $config_file --debug start + sleep 2 + ;; + 'stop') echo "Stopping the LiveSupport scheduler..." $scheduler_exe -c $config_file stop @@ -113,7 +119,7 @@ case "$mode" in echo "LiveSupport scheduler System V runlevel init script." echo "" echo "Usage:" - echo " $0 start|stop|status|install|uninstall|kill" + echo " $0 start|run|stop|status|install|uninstall|kill" echo "" esac diff --git a/livesupport/products/scheduler/etc/Makefile.in b/livesupport/products/scheduler/etc/Makefile.in index 153b75b24..d5afd2f37 100644 --- a/livesupport/products/scheduler/etc/Makefile.in +++ b/livesupport/products/scheduler/etc/Makefile.in @@ -21,7 +21,7 @@ # # # Author : $Author: maroy $ -# Version : $Revision: 1.52 $ +# Version : $Revision: 1.53 $ # Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/etc/Makefile.in,v $ # # @configure_input@ @@ -122,6 +122,7 @@ SCHEDULER_SH = ${BIN_DIR}/scheduler_devenv.sh SCHEDULER_CFG = ${ETC_DIR}/scheduler.xml SCHEDULER_WEB_CFG = ${ETC_DIR}/scheduler-web.xml TEST_RUNNER = ${TMP_DIR}/testRunner +TEST_RUNNER_SH = ${BIN_DIR}/run_tests.sh DOXYGEN_CONFIG = ${ETC_DIR}/doxygen.config @@ -295,13 +296,12 @@ depclean: clean distclean: clean docclean ${RMDIR} ${TMP_DIR}/config* ${TMP_DIR}/autom4te* -check: all ${TEST_RUNNER} storage_server_init start run_tests stop +check: all ${TEST_RUNNER} storage_server_init start run run_tests stop check_local: all ${TEST_RUNNER} start_local run_tests stop_local run_tests: ${TEST_RUNNER} - ./bin/run_tests.sh -o ${TEST_RESULTS} -s ${TEST_XSLT} -# ${TEST_RUNNER} -o ${TEST_RESULTS} -s ${TEST_XSLT} + ${TEST_RUNNER_SH} -o ${TEST_RESULTS} -s ${TEST_XSLT} install: ${SCHEDULER_EXE} ${SCHEDULER_SH} install @@ -310,6 +310,9 @@ start: ${SCHEDULER_EXE} ${SCHEDULER_SH} start sleep 2 +run: ${SCHEDULER_EXE} + ${SCHEDULER_SH} run + stop: ${SCHEDULER_EXE} ${SCHEDULER_SH} stop sleep 2 diff --git a/livesupport/products/scheduler/src/PlaylistEvent.cxx b/livesupport/products/scheduler/src/PlaylistEvent.cxx index c4f8e87a3..42a130444 100644 --- a/livesupport/products/scheduler/src/PlaylistEvent.cxx +++ b/livesupport/products/scheduler/src/PlaylistEvent.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.7 $ + Version : $Revision: 1.8 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/PlaylistEvent.cxx,v $ ------------------------------------------------------------------------------*/ @@ -81,6 +81,8 @@ PlaylistEvent :: PlaylistEvent( // this init time is a wild guess, say 5 seconds should be enough initTime.reset(new posix_time::time_duration(0, 0, 5, 0)); + + state = created; } @@ -90,10 +92,16 @@ PlaylistEvent :: PlaylistEvent( void PlaylistEvent :: initialize(void) throw (std::exception) { + if (state != created) { + throw std::logic_error("PlaylistEvent in bad state"); + } + + state = initializing; // some ugliness because getPlaylistId() returns a const pointer Ptr::Ref playlistId(new UniqueId(scheduleEntry->getPlaylistId() ->getId())); playlist = storage->acquirePlaylist(sessionId, playlistId); + state = initialized; } @@ -103,8 +111,14 @@ PlaylistEvent :: initialize(void) throw (std::exception) void PlaylistEvent :: deInitialize(void) throw () { + if (state != stopped) { + // TODO: handle error? + return; + } + storage->releasePlaylist(sessionId, playlist); playlist.reset(); + state = deInitialized; } @@ -114,6 +128,11 @@ PlaylistEvent :: deInitialize(void) throw () void PlaylistEvent :: start(void) throw () { + if (state != initialized) { + // TODO: handle error? + return; + } + try { audioPlayer->open(*playlist->getUri()); audioPlayer->start(); @@ -123,6 +142,7 @@ PlaylistEvent :: start(void) throw () std::cerr << e.what() << std::endl; // TODO: handle error? } + state = running; } @@ -132,7 +152,13 @@ PlaylistEvent :: start(void) throw () void PlaylistEvent :: stop(void) throw () { + if (state != running) { + // TODO: handle error? + return; + } + audioPlayer->stop(); audioPlayer->close(); + state = stopped; } diff --git a/livesupport/products/scheduler/src/PlaylistEvent.h b/livesupport/products/scheduler/src/PlaylistEvent.h index ef2311a66..9e6872a28 100644 --- a/livesupport/products/scheduler/src/PlaylistEvent.h +++ b/livesupport/products/scheduler/src/PlaylistEvent.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.7 $ + Version : $Revision: 1.8 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/PlaylistEvent.h,v $ ------------------------------------------------------------------------------*/ @@ -72,10 +72,21 @@ using namespace LiveSupport::Storage; * A scheduled event for playing a playlist. * * @author $Author: maroy $ - * @version $Revision: 1.7 $ + * @version $Revision: 1.8 $ */ class PlaylistEvent : public virtual ScheduledEventInterface { + private: + /** + * Enumeration describing the possible states of the event. + */ + typedef enum { created, + initializing, + initialized, + running, + stopped, + deInitialized } State; + private: /** * The audio player to play the playlist with. @@ -95,23 +106,28 @@ class PlaylistEvent : public virtual ScheduledEventInterface /** * The schedule entry this event is playing. */ - Ptr::Ref scheduleEntry; + Ptr::Ref scheduleEntry; /** * The maximum time this event should get initialized in. */ - Ptr::Ref initTime; + Ptr::Ref initTime; /** * The Playlist this event is playing. */ - Ptr::Ref playlist; + Ptr::Ref playlist; /** * The session ID used for authentication at the storage server. */ - Ptr::Ref sessionId; + Ptr::Ref sessionId; + /** + * The current state of the event. + */ + State state; + public: /** diff --git a/livesupport/products/scheduler/src/RpcUploadPlaylistTest.cxx b/livesupport/products/scheduler/src/RpcUploadPlaylistTest.cxx index 4ae8f5972..ec5ff29a3 100644 --- a/livesupport/products/scheduler/src/RpcUploadPlaylistTest.cxx +++ b/livesupport/products/scheduler/src/RpcUploadPlaylistTest.cxx @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.10 $ + Version : $Revision: 1.11 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/RpcUploadPlaylistTest.cxx,v $ ------------------------------------------------------------------------------*/ @@ -168,3 +168,45 @@ RpcUploadPlaylistTest :: simpleTest(void) xmlRpcClient.close(); } + +/*------------------------------------------------------------------------------ + * A test to try to schedule something that would have to have + * been already initialized (thus, the start time is in the + * future, but the initialize time is already in the past.) + * see http://bugs.campware.org/view.php?id=757 + *----------------------------------------------------------------------------*/ +void +RpcUploadPlaylistTest :: postInitTest(void) + throw (CPPUNIT_NS::Exception) +{ + XmlRpc::XmlRpcValue parameters; + XmlRpc::XmlRpcValue result; + struct tm time; + + XmlRpc::XmlRpcClient xmlRpcClient(getXmlRpcHost().c_str(), + getXmlRpcPort(), + "/RPC2", + false); + + // first, get the scheduler time + result.clear(); + xmlRpcClient.execute("getSchedulerTime", parameters, result); + CPPUNIT_ASSERT(!xmlRpcClient.isFault()); + CPPUNIT_ASSERT(result.hasMember("schedulerTime")); + time = result["schedulerTime"]; + + // try to schedule playlist #1 in 4 seconds from now + parameters.clear(); + parameters["sessionId"] = sessionId->getId(); + parameters["playlistId"] = "0000000000000001"; + // TODO: hopefully time conversion will handle seconds > 60 OK + time.tm_sec += 4; + parameters["playtime"] = &time; + + result.clear(); + xmlRpcClient.execute("uploadPlaylist", parameters, result); + CPPUNIT_ASSERT(!xmlRpcClient.isFault()); + + xmlRpcClient.close(); +} + diff --git a/livesupport/products/scheduler/src/RpcUploadPlaylistTest.h b/livesupport/products/scheduler/src/RpcUploadPlaylistTest.h index 5fba4c1f0..a16db2f48 100644 --- a/livesupport/products/scheduler/src/RpcUploadPlaylistTest.h +++ b/livesupport/products/scheduler/src/RpcUploadPlaylistTest.h @@ -22,7 +22,7 @@ Author : $Author: maroy $ - Version : $Revision: 1.4 $ + Version : $Revision: 1.5 $ Location : $Source: /home/paul/cvs2svn-livesupport/newcvsrepo/livesupport/products/scheduler/src/RpcUploadPlaylistTest.h,v $ ------------------------------------------------------------------------------*/ @@ -64,13 +64,14 @@ using namespace LiveSupport::Core; * Unit test to test the uploadPlaylist XML-RPC call. * * @author $Author: maroy $ - * @version $Revision: 1.4 $ + * @version $Revision: 1.5 $ * @see SchedulerDaemon */ class RpcUploadPlaylistTest : public BaseTestMethod { CPPUNIT_TEST_SUITE(RpcUploadPlaylistTest); CPPUNIT_TEST(simpleTest); + CPPUNIT_TEST(postInitTest); CPPUNIT_TEST_SUITE_END(); private: @@ -90,6 +91,17 @@ class RpcUploadPlaylistTest : public BaseTestMethod void simpleTest(void) throw (CPPUNIT_NS::Exception); + /** + * A test to try to schedule something that would have to have + * been already initialized (thus, the start time is in the + * future, but the initialize time is already in the past.) + * see http://bugs.campware.org/view.php?id=757 + * + * @exception CPPUNIT_NS::Exception on test failures. + */ + void + postInitTest(void) throw (CPPUNIT_NS::Exception); + public: /**