bosun.org@v0.0.0-20210513094433-e25bc3e69a1f/cmd/bosun/database/test/notifications_test.go (about)

     1  package dbtest
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"bosun.org/models"
     8  )
     9  
    10  func TestNotifications_RoundTrip(t *testing.T) {
    11  
    12  	nd := testData.Notifications()
    13  	notTime := time.Now().UTC().Add(-10 * time.Hour).Truncate(time.Second)
    14  	future := time.Now().UTC().Add(time.Hour).Truncate(time.Second)
    15  	oneMin := time.Now().UTC().Add(time.Minute).Truncate(time.Second)
    16  
    17  	// with nothing pending, next time should be an hour from now
    18  	next, err := nd.GetNextNotificationTime()
    19  	check(t, err)
    20  	if next != future {
    21  		t.Fatalf("wrong next time. %s != %s", next, future)
    22  	}
    23  
    24  	// add notifications
    25  	err = nd.InsertNotification(models.AlertKey("notak{foo=a}"), "chat", notTime)
    26  	check(t, err)
    27  	err = nd.InsertNotification(models.AlertKey("notak{foo=b}"), "chat", oneMin)
    28  	check(t, err)
    29  	err = nd.InsertNotification(models.AlertKey("notak{foo=c}"), "chat", future)
    30  	check(t, err)
    31  
    32  	// next time should be correct
    33  	next, err = nd.GetNextNotificationTime()
    34  	check(t, err)
    35  	if next != notTime {
    36  		t.Fatalf("wrong next time. %s != %s", next, notTime)
    37  	}
    38  
    39  	// make sure only one due
    40  	due, err := nd.GetDueNotifications()
    41  	check(t, err)
    42  	if len(due) != 1 {
    43  		t.Fatalf("Wrong number of due notifications. %d != %d", len(due), 1)
    44  	}
    45  
    46  	// next time should still be correct
    47  	next, err = nd.GetNextNotificationTime()
    48  	check(t, err)
    49  	if next != notTime {
    50  		t.Fatalf("wrong next time. %s != %s", next, notTime)
    51  	}
    52  
    53  	check(t, nd.ClearNotificationsBefore(notTime))
    54  	// next time should be 1 minute
    55  	next, err = nd.GetNextNotificationTime()
    56  	check(t, err)
    57  	if next != oneMin {
    58  		t.Fatalf("wrong next time. %s != %s", next, oneMin)
    59  	}
    60  
    61  	check(t, nd.ClearNotifications(models.AlertKey("notak{foo=b}")))
    62  	// next time should be 1 hour
    63  	next, err = nd.GetNextNotificationTime()
    64  	check(t, err)
    65  	if next != future {
    66  		t.Fatalf("wrong next time. %s != %s", next, future)
    67  	}
    68  }