# Path: axiom/iaxiom.py
# class IScheduler(Interface):
#     """
#     An interface for scheduling tasks.  Quite often the store will be adaptable
#     to this; in any Mantissa application, for example; so it is reasonable to
#     assume that it is if your application needs to schedule timed events or
#     queue tasks.
#     """
#     def schedule(runnable, when):
#         """
#         @param runnable: any Item with a 'run' method.
# 
#         @param when: a Time instance describing when the runnable's run()
#         method will be called.  See extime.Time's documentation for more
#         details.
#         """
# 
# Path: axiom/scheduler.py
# class Scheduler(Item, _SchedulerCompatMixin):
#     """
#     Track and execute persistent timed events for a I{site} store.
# 
#     This is deprecated and present only for backwards compatibility.  Adapt
#     the store to L{IScheduler} instead.
#     """
#     typeName = 'axiom_scheduler'
#     schemaVersion = 2
# 
#     dummy = integer()
# 
#     def activate(self):
#         _SchedulerCompatMixin.activate(self)
# 
# 
#     def setServiceParent(self, parent):
#         """
#         L{Scheduler} is no longer an L{IService}, but still provides this
#         method as a no-op in case an instance which was still an L{IService}
#         powerup is loaded (in which case it will be used like a service
#         once).
#         """
# 
# class _SiteScheduler(SchedulerMixin, Service, object):
#     """
#     Adapter from a site store to L{IScheduler}.
#     """
#     timer = None
#     callLater = reactor.callLater
#     now = Time
# 
#     def __init__(self, store):
#         self.store = store
#         self.setName(SITE_SCHEDULER)
# 
# 
#     def startService(self):
#         """
#         Start calling persistent timed events whose time has come.
#         """
#         super(_SiteScheduler, self).startService()
#         self._transientSchedule(self.now(), self.now())
# 
# 
#     def stopService(self):
#         """
#         Stop calling persistent timed events.
#         """
#         super(_SiteScheduler, self).stopService()
#         if self.timer is not None:
#             self.timer.cancel()
#             self.timer = None
# 
# 
#     def tick(self):
#         self.timer = None
#         return super(_SiteScheduler, self).tick()
# 
# 
#     def _transientSchedule(self, when, now):
#         """
#         If the service is currently running, schedule a tick to happen no
#         later than C{when}.
# 
#         @param when: The time at which to tick.
#         @type when: L{epsilon.extime.Time}
# 
#         @param now: The current time.
#         @type now: L{epsilon.extime.Time}
#         """
#         if not self.running:
#             return
#         if self.timer is not None:
#             if self.timer.getTime() < when.asPOSIXTimestamp():
#                 return
#             self.timer.cancel()
#         delay = when.asPOSIXTimestamp() - now.asPOSIXTimestamp()
# 
#         # reactor.callLater allows only positive delay values.  The scheduler
#         # may want to have scheduled things in the past and that's OK, since we
#         # are dealing with Time() instances it's impossible to predict what
#         # they are relative to the current time from user code anyway.
#         delay = max(_EPSILON, delay)
#         self.timer = self.callLater(delay, self.tick)
#         self.nextEventAt = when


from axiom.iaxiom import IScheduler
from axiom.scheduler import Scheduler, _SiteScheduler
from axiom.test.historic.stubloader import StubbedTest

"""
Tests for the upgrade of Scheduler from version 1 to version 2, in which it was
largely supplanted by L{_SiteScheduler}.
"""



class SchedulerUpgradeTests(StubbedTest):
    def test_powerdown(self):
        """
        The L{Scheduler} created by the stub is powered down by the upgrade and
        adapting the L{Store} to L{IScheduler} succeeds with an instance of
        L{_SiteScheduler}.
        """
        scheduler = self.store.findUnique(Scheduler)
        self.assertEqual(list(self.store.interfacesFor(scheduler)), [])

# Path: axiom/iaxiom.py
# class IScheduler(Interface):
#     """
#     An interface for scheduling tasks.  Quite often the store will be adaptable
#     to this; in any Mantissa application, for example; so it is reasonable to
#     assume that it is if your application needs to schedule timed events or
#     queue tasks.
#     """
#     def schedule(runnable, when):
#         """
#         @param runnable: any Item with a 'run' method.
# 
#         @param when: a Time instance describing when the runnable's run()
#         method will be called.  See extime.Time's documentation for more
#         details.
#         """
# 
# Path: axiom/scheduler.py
# class Scheduler(Item, _SchedulerCompatMixin):
#     """
#     Track and execute persistent timed events for a I{site} store.
# 
#     This is deprecated and present only for backwards compatibility.  Adapt
#     the store to L{IScheduler} instead.
#     """
#     typeName = 'axiom_scheduler'
#     schemaVersion = 2
# 
#     dummy = integer()
# 
#     def activate(self):
#         _SchedulerCompatMixin.activate(self)
# 
# 
#     def setServiceParent(self, parent):
#         """
#         L{Scheduler} is no longer an L{IService}, but still provides this
#         method as a no-op in case an instance which was still an L{IService}
#         powerup is loaded (in which case it will be used like a service
#         once).
#         """
# 
# class _SiteScheduler(SchedulerMixin, Service, object):
#     """
#     Adapter from a site store to L{IScheduler}.
#     """
#     timer = None
#     callLater = reactor.callLater
#     now = Time
# 
#     def __init__(self, store):
#         self.store = store
#         self.setName(SITE_SCHEDULER)
# 
# 
#     def startService(self):
#         """
#         Start calling persistent timed events whose time has come.
#         """
#         super(_SiteScheduler, self).startService()
#         self._transientSchedule(self.now(), self.now())
# 
# 
#     def stopService(self):
#         """
#         Stop calling persistent timed events.
#         """
#         super(_SiteScheduler, self).stopService()
#         if self.timer is not None:
#             self.timer.cancel()
#             self.timer = None
# 
# 
#     def tick(self):
#         self.timer = None
#         return super(_SiteScheduler, self).tick()
# 
# 
#     def _transientSchedule(self, when, now):
#         """
#         If the service is currently running, schedule a tick to happen no
#         later than C{when}.
# 
#         @param when: The time at which to tick.
#         @type when: L{epsilon.extime.Time}
# 
#         @param now: The current time.
#         @type now: L{epsilon.extime.Time}
#         """
#         if not self.running:
#             return
#         if self.timer is not None:
#             if self.timer.getTime() < when.asPOSIXTimestamp():
#                 return
#             self.timer.cancel()
#         delay = when.asPOSIXTimestamp() - now.asPOSIXTimestamp()
# 
#         # reactor.callLater allows only positive delay values.  The scheduler
#         # may want to have scheduled things in the past and that's OK, since we
#         # are dealing with Time() instances it's impossible to predict what
#         # they are relative to the current time from user code anyway.
#         delay = max(_EPSILON, delay)
#         self.timer = self.callLater(delay, self.tick)
#         self.nextEventAt = when


# Path: axiom/test/historic/test_scheduler1to2.py
from axiom.iaxiom import IScheduler
from axiom.scheduler import Scheduler, _SiteScheduler
from axiom.test.historic.stubloader import StubbedTest


"""
Tests for the upgrade of Scheduler from version 1 to version 2, in which it was
largely supplanted by L{_SiteScheduler}.
"""



class SchedulerUpgradeTests(StubbedTest):
    def test_powerdown(self):
        """
        The L{Scheduler} created by the stub is powered down by the upgrade and
        adapting the L{Store} to L{IScheduler} succeeds with an instance of
        L{_SiteScheduler}.
        """
        scheduler = self.store.findUnique(Scheduler)
        self.assertEqual(list(self.store.interfacesFor(scheduler)), [])

        self.assertIsInstance(IScheduler(self.store), _SiteScheduler)