github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/alerts/alerts_test.go (about)

     1  package alerts
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/evergreen-ci/evergreen/db"
     8  	"github.com/evergreen-ci/evergreen/model/alert"
     9  	"github.com/evergreen-ci/evergreen/testutil"
    10  	. "github.com/smartystreets/goconvey/convey"
    11  	"gopkg.in/mgo.v2/bson"
    12  )
    13  
    14  var testConf = testutil.TestConfig()
    15  
    16  func init() {
    17  	db.SetGlobalSessionProvider(db.SessionFactoryFromConfig(testConf))
    18  }
    19  
    20  func TestAlertQueue(t *testing.T) {
    21  	Convey("After queueing a few alerts", t, func() {
    22  		testutil.HandleTestingErr(db.Clear(alert.Collection), t, "problem clearing collections")
    23  		now := time.Now()
    24  
    25  		// a bunch of alerts, in order of oldest to newest
    26  		testAlerts := []*alert.AlertRequest{
    27  			{Id: bson.NewObjectId(), Display: "test1", CreatedAt: now.Add(time.Millisecond)},
    28  			{Id: bson.NewObjectId(), Display: "test2", CreatedAt: now.Add(2 * time.Millisecond)},
    29  			{Id: bson.NewObjectId(), Display: "test3", CreatedAt: now.Add(3 * time.Millisecond)},
    30  		}
    31  
    32  		for _, a := range testAlerts {
    33  			err := alert.EnqueueAlertRequest(a)
    34  			So(err, ShouldBeNil)
    35  		}
    36  
    37  		Convey("dequeuing should return them in order, oldest first", func() {
    38  			found := 0
    39  			for {
    40  				nextAlert, err := alert.DequeueAlertRequest()
    41  				So(err, ShouldBeNil)
    42  				if nextAlert == nil {
    43  					break
    44  				}
    45  				So(nextAlert.Display, ShouldEqual, testAlerts[found].Display)
    46  				So(nextAlert.QueueStatus, ShouldEqual, alert.InProgress)
    47  				found++
    48  			}
    49  			So(found, ShouldEqual, len(testAlerts))
    50  		})
    51  	})
    52  }