Repository: prometheus/alertmanager
File: types/types_test.go (Go)

Code before:
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(2 * time.Minute),
		Labels:   model.LabelSet{"test": "suppressed"},
	}
	a3 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(-1 * time.Minute),
		Labels:   model.LabelSet{"test": "resolved"},
	}

	// Insert an active alert.
	marker.SetActiveOrSilenced(a1.Fingerprint(), 1, nil, nil)
	require.Equal(t, 1, countByState(AlertStateActive))
	require.Equal(t, 1, countTotal())

	// Insert a suppressed alert by silence.
	marker.SetActiveOrSilenced(a2.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 1, countByState(AlertStateSuppressed))
	require.Equal(t, 2, countTotal())

	// Insert a resolved silenced alert - it'll count as suppressed.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 2, countByState(AlertStateSuppressed))
	require.Equal(t, 3, countTotal())

	// Insert a resolved alert - it'll count as active.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, nil, nil)
	require.Equal(t, 2, countByState(AlertStateActive))
	require.Equal(t, 3, countTotal())
}

func TestAlertMerge(t *testing.T) {
	now := time.Now()

	// By convention, alert A is always older than alert B.
	pairs := []struct {
		A, B, Res *Alert
	}{
		{
			// Both alerts have the Timeout flag set.
			// StartsAt is defined by Alert A.
			// EndsAt is defined by Alert B.
			A: &Alert{
				Alert: model.Alert{
					StartsAt: now.Add(-2 * time.Minute),
					EndsAt:   now.Add(2 * time.Minute),
				},
				UpdatedAt: now,
				Timeout:   true,
			},
			B: &Alert{

Review comment (suggestion):
I think this better explains what is happening here, what do you think?

```suggestion
	// Remove the silence for a3 - it'll count as active.
```

Code after:
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(2 * time.Minute),
		Labels:   model.LabelSet{"test": "suppressed"},
	}
	a3 := model.Alert{
		StartsAt: now.Add(-2 * time.Minute),
		EndsAt:   now.Add(-1 * time.Minute),
		Labels:   model.LabelSet{"test": "resolved"},
	}

	// Insert an active alert.
	marker.SetActiveOrSilenced(a1.Fingerprint(), 1, nil, nil)
	require.Equal(t, 1, countByState(AlertStateActive))
	require.Equal(t, 1, countTotal())

	// Insert a silenced alert.
	marker.SetActiveOrSilenced(a2.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 1, countByState(AlertStateSuppressed))
	require.Equal(t, 2, countTotal())

	// Insert a resolved silenced alert - it'll count as suppressed.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, []string{"1"}, nil)
	require.Equal(t, 2, countByState(AlertStateSuppressed))
	require.Equal(t, 3, countTotal())

	// Remove the silence from a3 - it'll count as active.
	marker.SetActiveOrSilenced(a3.Fingerprint(), 1, nil, nil)
	require.Equal(t, 2, countByState(AlertStateActive))
	require.Equal(t, 3, countTotal())
}

func TestAlertMerge(t *testing.T) {
	now := time.Now()

	// By convention, alert A is always older than alert B.
	pairs := []struct {
		A, B, Res *Alert
	}{
		{
			// Both alerts have the Timeout flag set.
			// StartsAt is defined by Alert A.
			// EndsAt is defined by Alert B.
			A: &Alert{
				Alert: model.Alert{
					StartsAt: now.Add(-2 * time.Minute),
					EndsAt:   now.Add(2 * time.Minute),
				},
				UpdatedAt: now,
				Timeout:   true,
			},
			B: &Alert{