Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions PoshBot/Classes/ScheduledMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class ScheduledMessage {

[int]$TimesExecuted = 0

[System.Diagnostics.Stopwatch]$Stopwatch

[DateTime]$StartAfter = (Get-Date).ToUniversalTime()

ScheduledMessage([TimeInterval]$Interval, [int]$TimeValue, [Message]$Message, [bool]$Enabled, [DateTime]$StartAfter) {
Expand All @@ -45,7 +43,6 @@ class ScheduledMessage {
$this.Enabled = $true
$this.Once = $true
$this.StartAfter = $StartAt.ToUniversalTime()
$this.Stopwatch = New-Object -TypeName System.Diagnostics.Stopwatch
}

[void]Init([TimeInterval]$Interval, [int]$TimeValue, [Message]$Message, [bool]$Enabled, [DateTime]$StartAfter) {
Expand All @@ -54,7 +51,6 @@ class ScheduledMessage {
$this.Message = $Message
$this.Enabled = $Enabled
$this.StartAfter = $StartAfter.ToUniversalTime()
$this.Stopwatch = New-Object -TypeName System.Diagnostics.Stopwatch

switch ($this.TimeInterval) {
'Days' {
Expand All @@ -77,40 +73,25 @@ class ScheduledMessage {
}

[bool]HasElapsed() {
if ($this.Stopwatch.ElapsedMilliseconds -gt $this.IntervalMS) {
$now = (Get-Date).ToUniversalTime()
if ($now -ge $this.StartAfter) {
$this.TimesExecuted += 1
return $true
} else{
return $false
}
$now = (Get-Date).ToUniversalTime()
if ($now -gt $this.StartAfter) {
$this.TimesExecuted += 1
return $true
} else {
return $false
}
}

[void]Enable() {
$this.Enabled = $true
$this.StartTimer()
}

[void]Disable() {
$this.Enabled = $false
$this.StopTimer()
}

[void]StartTimer() {
$this.Stopwatch.Start()
}

[void]StopTimer() {
$this.Stopwatch.Stop()
}

[void]ResetTimer() {
$this.Stopwatch.Reset()
$this.Stopwatch.Start()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the stopwatch is not longer need. Can you remove it from the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I removed it from the scheduler class. I did a search and the only other areas the stopwatch seems to be referenced were in the actual Bot instance for tracking uptime.

Let me know if you find anything else that requires changing!

[void]RecalculateStartAfter() {
$this.StartAfter = $this.StartAfter.AddMilliseconds($this.IntervalMS)
}

[hashtable]ToHash() {
Expand Down
5 changes: 1 addition & 4 deletions PoshBot/Classes/Scheduler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Scheduler : BaseLogger {
$_.Value.StopTimer()
$remove += $_.Value.Id
} else {
$_.Value.ResetTimer()
$_.Value.RecalculateStartAfter()
}

$newMsg = $_.Value.Message.Clone()
Expand Down Expand Up @@ -133,9 +133,6 @@ class Scheduler : BaseLogger {
$existingMessage = $this.GetSchedule($ScheduledMessage.Id)
$existingMessage.Init($ScheduledMessage.TimeInterval, $ScheduledMessage.TimeValue, $ScheduledMessage.Message, $ScheduledMessage.Enabled, $ScheduledMessage.StartAfter)
$this.LogInfo("Scheduled message [$($ScheduledMessage.Id)] modified", $existingMessage)
if ($existingMessage.Enabled) {
$existingMessage.ResetTimer()
}

$this.SaveState()
return $existingMessage
Expand Down
158 changes: 158 additions & 0 deletions Tests/Unit/Classes/ScheduledMessages.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
InModuleScope PoshBot {

Describe ScheduledMessage {
# Mock a message
$msg = [Message]::new()
$msg.Id = 1
$msg.Text = "This is a test message"

# Call constructor
$Interval = 'Days'
$TimeValue = 1
$Message = $msg
$StartAfter = (Get-Date).AddMinutes(1)

Context "Constructor: String, Int, Message, DateTime" {

$ScheduledMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message, $StartAfter)

It 'TimeInterval should match argument' {
$ScheduledMessage.TimeInterval | Should be $Interval
}

It 'TimeValue should match argument' {
$ScheduledMessage.TimeValue | Should be $TimeValue
}

It 'Message Id should be 1' {
$ScheduledMessage.Message.Id | Should be 1
}
It 'Message Text should match argument' {
$ScheduledMessage.Message.Text | Should match $msg.Text
}

It 'StartAfter value should match argument converted to UTC' {
$ScheduledMessage.StartAfter | Should be $StartAfter.ToUniversalTime()
}
}

Context "Constructor: String, Int, Message" {

$ScheduledMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message)

It 'TimeInterval should match argument' {
$ScheduledMessage.TimeInterval | Should be $Interval
}

It 'TimeValue should match argument' {
$ScheduledMessage.TimeValue | Should be $TimeValue
}

It 'Message Id should be 1' {
$ScheduledMessage.Message.Id | Should be 1
}
It 'Message Text should match argument' {
$ScheduledMessage.Message.Text | Should match $msg.Text
}

It 'Should have a StartAfter value without passing the argument' {
$ScheduledMessage.StartAfter | Should not BeNullOrEmpty
}
}

Context "Methods: Init()" {

$Intervals = @(
@{
Interval = 'Days'
MS = 86400000
},
@{
Interval = 'Hours'
MS = 3600000
},
@{
Interval = 'Minutes'
MS = 60000
},
@{
Interval = 'Seconds'
MS = 1000
}
)

foreach ($Type in $Intervals) {
It "Should calculate milliseconds for single $($Type.Interval) correctly" {
$ScheduledMessage = [ScheduledMessage]::new($Type.Interval, 1, $Message)
$ScheduledMessage.IntervalMS | Should be (1 * $Type.MS)
}

It "Should calculate milliseconds for multiple $($Type.Interval) correctly" {
$ScheduledMessage = [ScheduledMessage]::new($Type.Interval, 5, $Message)
$ScheduledMessage.IntervalMS | Should be (5 * $Type.MS)
}
}

}

Context "Method: HasElapsed()" {
$ElapsedMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message, (Get-Date).AddHours(-5))
$ScheduledMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message, (Get-Date).AddHours(5))

It 'Should return true when past the StartAfter DateTime' {
$ElapsedMessage.HasElapsed() | Should BeTrue
}

It 'Should return false when before the StartAfter DateTime' {
$ScheduledMessage.HasElapsed() | Should Not BeTrue
}

It 'TimesExecuted should be 1 after executing' {
$ElapsedMessage.TimesExecuted | Should be 1
}

}

Context "Method: Disable()" {
$ScheduledMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message, (Get-Date))

It 'Disables the instance when called' {
$ScheduledMessage.Disable()
$SceduleMessage.Enabled | Should Not BeTrue
}

It 'Does not throw an error when being called on an already-disabled instance' {
$ScheduledMessage.Disable()
$ScheduledMessage.Enabled | Should Not BeTrue
}
}

Context "Method: Enable()" {
$ScheduledMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message, (Get-Date))

It 'Enables the instance when called' {
$ScheduledMessage.Disable()
$ScheduledMessage.Enable()
$ScheduledMessage.Enabled | Should BeTrue
}

It 'Does not throw an error when being called on an already-enabled instance' {
$ScheduledMessage.Enable()
$ScheduledMessage.Enabled | Should BeTrue
}
}

Context "Method: RecalculateStartAfter()" {
$ScheduledMessage = [ScheduledMessage]::new($Interval, $TimeValue, $Message, (Get-Date))

It 'Should increase the StartAfter property by IntervalMS' {
$StartingValue = $ScheduledMessage.StartAfter
$ScheduledMessage.RecalculateStartAfter()

(New-TimeSpan $StartingValue $ScheduledMessage.StartAfter).TotalDays | Should Be 1
}
}

}

}