Explorar o código

Merge pull request #3 from cojac/entries_bug

Fixed bug when calling Entries()
Rob Figueiredo %!s(int64=12) %!d(string=hai) anos
pai
achega
483223747f
Modificáronse 2 ficheiros con 29 adicións e 0 borrados
  1. 4 0
      cron.go
  2. 25 0
      cron_test.go

+ 4 - 0
cron.go

@@ -155,6 +155,7 @@ func (c *Cron) run() {
 				e.Prev = e.Next
 				e.Next = e.Schedule.Next(effective)
 			}
+			continue
 
 		case newEntry := <-c.add:
 			c.entries = append(c.entries, newEntry)
@@ -166,6 +167,9 @@ func (c *Cron) run() {
 		case <-c.stop:
 			return
 		}
+
+		// 'now' should be updated after newEntry and snapshot cases.
+		now = time.Now().Local()
 	}
 }
 

+ 25 - 0
cron_test.go

@@ -77,6 +77,31 @@ func TestAddWhileRunning(t *testing.T) {
 	}
 }
 
+// Test timing with Entries.
+func TestSnapshotEntries(t *testing.T) {
+	wg := &sync.WaitGroup{}
+	wg.Add(1)
+
+	cron := New()
+	cron.AddFunc("@every 2s", func() { wg.Done() })
+	cron.Start()
+	defer cron.Stop()
+
+	// Cron should fire in 2 seconds. After 1 second, call Entries.
+	select {
+	case <-time.After(ONE_SECOND):
+		cron.Entries()
+	}
+
+	// Even though Entries was called, the cron should fire at the 2 second mark.
+	select {
+	case <-time.After(ONE_SECOND):
+		t.FailNow()
+	case <-wait(wg):
+	}
+
+}
+
 // Test that the entries are correctly sorted.
 // Add a bunch of long-in-the-future entries, and an immediate entry, and ensure
 // that the immediate entry runs immediately.