public async Task HandleTimerEvent_HandlesExceptions() { // force an exception to occur outside of the function invocation path var ex = new Exception("Kaboom!"); _mockScheduleMonitor.Setup(p => p.UpdateStatusAsync(_testTimerName, It.IsAny <ScheduleStatus>())).ThrowsAsync(ex); var listener = new TimerListener(_attribute, _schedule, _testTimerName, _options, _mockTriggerExecutor.Object, _logger, _mockScheduleMonitor.Object, _functionShortName); Assert.Null(listener.Timer); await listener.HandleTimerEvent(); // verify the timer was started Assert.NotNull(listener.Timer); Assert.True(listener.Timer.Enabled); var logs = _logger.GetLogMessages(); var log = logs[0]; Assert.Equal(LogLevel.Error, log.Level); Assert.Equal("Error occurred during scheduled invocation for 'TimerFunctionShortName'.", log.FormattedMessage); Assert.Same(ex, log.Exception); log = logs[1]; Assert.Equal(LogLevel.Debug, log.Level); Assert.True(log.FormattedMessage.StartsWith("Timer for 'TimerFunctionShortName' started with interval")); listener.Dispose(); }
public void Start() { //UI面板初始化 UIManager.getInstance().closePanel(PanelConfig.WORLDMAPPANEL); UIManager.getInstance().closePanel(PanelConfig.HOMECITYPANEL); //注册计时器,3,2,1,go,隐藏一共5次 timerListener = new TimerListener(this, 1, 5, false); //注册 XLMessageManager.getInstance().register(this); //进入战斗 XLMessageManager.getInstance().sendNotification(BATTLESTATE_START); //取得倒计时文本 GamePanel gamePanel = UIManager.getInstance().getPanel(PanelConfig.GAMEPANEL) as GamePanel; if (gamePanel != null) { timeText = gamePanel.timeText; } GameObject mainCamera = Game.GameEntry.Instance.Cameras.mainCamera.gameObject; iTween.MoveTo(mainCamera, new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, mainCamera.transform.position.z + 44), 5f); // 这样调节场景坐标,不管用 // GameObject sceneObject = GameObject.Find("GameObject"); // sceneObject.transform.position = new Vector3(sceneObject.transform.position.x, sceneObject.transform.position.y + 10f, sceneObject.transform.position.z); }
static void Main(string[] args) { Timer timer = new Timer(); TimerListener listener = new TimerListener(); listener.Subscribe(timer); timer.DoStuffEveryTTicks(190000); }
public void GetNextInterval_NegativeInterval_ReturnsOneTick() { var now = DateTime.Now; var next = now.Subtract(TimeSpan.FromSeconds(1)); var interval = TimerListener.GetNextTimerInterval(next, now); Assert.Equal(1, interval.Ticks); }
private void OnTimedEvent(object source, ElapsedEventArgs e) { //Debug.Log(this + " -> " + this.listeners.Count); aTimer.Stop(); for (int i = 0; i < listeners.Count; i++) { TimerListener timerListener = listeners[i]; timerListener.OnTimerComplete(this); } }
public Action <float> AddUpdate(Action <float> fun) { TimerListener tl = new TimerListener(); tl.onTime = fun; updates [fun] = tl; return(fun); }
private void ExtendCallbacksList() { if (_updateCallbacks == null) { _updateCallbacks = new TimerListener[10]; } else { TimerListener[] newCallbacks = new TimerListener[_updateCallbacks.Length + 10]; Array.Copy(_updateCallbacks, newCallbacks, _updateCallbacks.Length); _updateCallbacks = newCallbacks; } }
public void AddListener(int timeLeft, Action callback) { for (int i = 0; i < _updateCallbacks.Length; i++) { if (_updateCallbacks[i] == null) { _updateCallbacks[i] = new TimerListener(timeLeft, callback); return; } } ExtendCallbacksList(); AddListener(timeLeft, callback); }
public Action <float> Add(float interval, int repeat, Action <float> fun) { TimerListener tl = new TimerListener(); tl.interval = interval; tl.repeat = repeat; tl.onTime = fun; tl.isDelete = false; if (times.ContainsKey(fun)) { times [fun].isDelete = false; times.Remove(fun); } times [fun] = tl; return(fun); }
private void CreateTestListener(string expression, bool useMonitor = true) { _attribute = new TimerTriggerAttribute(expression); _attribute.UseMonitor = useMonitor; _config = new TimersConfiguration(); _mockScheduleMonitor = new Mock <ScheduleMonitor>(MockBehavior.Strict); _config.ScheduleMonitor = _mockScheduleMonitor.Object; _mockTriggerExecutor = new Mock <ITriggeredFunctionExecutor>(MockBehavior.Strict); FunctionResult result = new FunctionResult(true); _mockTriggerExecutor.Setup(p => p.TryExecuteAsync(It.IsAny <TriggeredFunctionData>(), It.IsAny <CancellationToken>())) .Callback <TriggeredFunctionData, CancellationToken>((mockFunctionData, mockToken) => { _triggeredFunctionData = mockFunctionData; }) .Returns(Task.FromResult(result)); _listener = new TimerListener(_attribute, _testTimerName, _config, _mockTriggerExecutor.Object); }
public void GetNextInterval_NextAfterDST_ReturnsExpectedValue() { // Running on the Friday before the DST switch at 2 AM on 3/11 (Pacific Standard Time) // Note: this test uses Local time, so if you're running in a timezone where // DST doesn't transition the test might not be valid. var now = new DateTime(2018, 3, 9, 18, 0, 0, DateTimeKind.Local); // Configure schedule to run again on the next Friday (3/16) at 6 PM (Pacific Daylight Time) var schedule = CrontabSchedule.Parse("0 0 18 * * 5", new CrontabSchedule.ParseOptions() { IncludingSeconds = true }); var next = schedule.GetNextOccurrence(now); var interval = TimerListener.GetNextTimerInterval(next, now); // One week is normally 168 hours, but it's 167 hours across DST Assert.Equal(167, interval.TotalHours); }
public void GetNextInterval_NextWithinDST_ReturnsExpectedValue(TimerSchedule schedule, TimeSpan expectedInterval) { // This only works with a DST-supported time zone, so throw a nice exception if (!TimeZoneInfo.Local.SupportsDaylightSavingTime) { throw new InvalidOperationException("This test will only pass if the time zone supports DST."); } // Running at 1:59 AM, i.e. one minute before the DST switch at 2 AM on 3/11 (Pacific Standard Time) // Note: this test uses Local time, so if you're running in a timezone where // DST doesn't transition the test might not be valid. var now = new DateTime(2018, 3, 11, 1, 59, 0, DateTimeKind.Local); var next = schedule.GetNextOccurrence(now); var interval = TimerListener.GetNextTimerInterval(next, now, schedule.AdjustForDST); Assert.Equal(expectedInterval, interval); }
private void CreateTestListener(string expression, bool useMonitor = true, Action functionAction = null) { _attribute = new TimerTriggerAttribute(expression); _schedule = TimerSchedule.Create(_attribute, new TestNameResolver()); _attribute.UseMonitor = useMonitor; _options = new TimersOptions(); _mockScheduleMonitor = new Mock <ScheduleMonitor>(MockBehavior.Strict); _mockTriggerExecutor = new Mock <ITriggeredFunctionExecutor>(MockBehavior.Strict); FunctionResult result = new FunctionResult(true); _mockTriggerExecutor.Setup(p => p.TryExecuteAsync(It.IsAny <TriggeredFunctionData>(), It.IsAny <CancellationToken>())) .Callback <TriggeredFunctionData, CancellationToken>((mockFunctionData, mockToken) => { _triggeredFunctionData = mockFunctionData; functionAction?.Invoke(); }) .Returns(Task.FromResult(result)); _logger = new TestLogger(null); _listener = new TimerListener(_attribute, _schedule, _testTimerName, _options, _mockTriggerExecutor.Object, _logger, _mockScheduleMonitor.Object); }
public void GetNextInterval_NextWithinDST_ReturnsExpectedValue() { // Running at 1:59 AM, i.e. one minute before the DST switch at 2 AM on 3/11 (Pacific Standard Time) // Note: this test uses Local time, so if you're running in a timezone where // DST doesn't transition the test might not be valid. var now = new DateTime(2018, 3, 11, 1, 59, 0, DateTimeKind.Local); // Configure schedule to run on the 59th minute of every hour var schedule = CrontabSchedule.Parse("0 59 * * * *", new CrontabSchedule.ParseOptions() { IncludingSeconds = true }); // Note: NCronTab actually gives us an invalid next occurrence of 2:59 AM, which doesn't actually // exist because the DST switch skips from 2 to 3 var next = schedule.GetNextOccurrence(now); var interval = TimerListener.GetNextTimerInterval(next, now); Assert.Equal(1, interval.TotalHours); }
public void GetNextInterval_NextAfterDST_ReturnsExpectedValue(TimerSchedule schedule, TimeSpan expectedInterval) { // This only works with a DST-supported time zone, so throw a nice exception if (!TimeZoneInfo.Local.SupportsDaylightSavingTime) { throw new InvalidOperationException("This test will only pass if the time zone supports DST."); } // Running on the Friday before the DST switch at 2 AM on 3/11 (Pacific Standard Time) // Note: this test uses Local time, so if you're running in a timezone where // DST doesn't transition the test might not be valid. // The input schedules will run after DST changes. For some (Cron), they will subtract // an hour to account for the shift. For others (Constant), they will not. var now = new DateTime(2018, 3, 9, 18, 0, 0, DateTimeKind.Local); var next = schedule.GetNextOccurrence(now); var interval = TimerListener.GetNextTimerInterval(next, now, schedule.AdjustForDST); // One week is normally 168 hours, but it's 167 hours across DST Assert.Equal(interval, expectedInterval); }
public void RemoveListener(TimerListener newListener) { listeners.Remove(newListener); }
public void AddListener(TimerListener newListener) { listeners.Add(newListener); //Debug.Log("AddListener" + this + " -> " + this.listeners.Count); }