...

Source file src/github.com/datawire/ambassador/v2/pkg/acp/diagd_test.go

Documentation: github.com/datawire/ambassador/v2/pkg/acp

     1  package acp_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/datawire/ambassador/v2/pkg/acp"
     8  	"github.com/datawire/dlib/dtime"
     9  )
    10  
    11  type diagdMetadata struct {
    12  	t  *testing.T
    13  	ft *dtime.FakeTime
    14  	dw *acp.DiagdWatcher
    15  }
    16  
    17  func (m *diagdMetadata) check(seq int, clock int, alive bool, ready bool) {
    18  	if m.ft.TimeSinceBoot() != time.Duration(clock)*time.Second {
    19  		m.t.Errorf("%d: fakeTime.TimeSinceBoot should be %ds, not %v", seq, clock, m.ft.TimeSinceBoot())
    20  	}
    21  
    22  	if m.dw.IsAlive() != alive {
    23  		m.t.Errorf("%d: DiagdWatcher.IsAlive %t, wanted %t", seq, m.dw.IsAlive(), alive)
    24  	}
    25  
    26  	if m.dw.IsReady() != ready {
    27  		m.t.Errorf("%d: DiagdWatcher.IsReady %t, wanted %t", seq, m.dw.IsReady(), ready)
    28  	}
    29  }
    30  
    31  func (m *diagdMetadata) stepSec(step int) {
    32  	m.ft.StepSec(step)
    33  }
    34  
    35  func newDiagdMetadata(t *testing.T) *diagdMetadata {
    36  	ft := dtime.NewFakeTime()
    37  
    38  	dw := acp.NewDiagdWatcher()
    39  	dw.SetFetchTime(ft.Now)
    40  
    41  	if dw == nil {
    42  		t.Error("New DiagdWatcher is nil?")
    43  	}
    44  
    45  	bootGraceLength := dw.GraceEnd.Sub(ft.BootTime())
    46  	tenMinutes := time.Minute * 10
    47  
    48  	if bootGraceLength != tenMinutes {
    49  		t.Errorf("GraceEnd is %v after bootTime, not %v", bootGraceLength, tenMinutes)
    50  	}
    51  
    52  	return &diagdMetadata{t: t, ft: ft, dw: dw}
    53  }
    54  
    55  func TestDiagdHappyPath(t *testing.T) {
    56  	m := newDiagdMetadata(t)
    57  	m.check(0, 0, true, false)
    58  
    59  	// Advance the clock 10s.
    60  	m.stepSec(10)
    61  
    62  	m.check(1, 10, true, false)
    63  
    64  	// Send a snapshot. We should still be alive but not ready.
    65  	m.dw.NoteSnapshotSent()
    66  
    67  	m.check(2, 10, true, false)
    68  
    69  	// Advance another 30s.
    70  	m.stepSec(30)
    71  
    72  	// Mark the snapshot processed. We should now be ready.
    73  	m.dw.NoteSnapshotProcessed()
    74  	m.check(3, 40, true, true)
    75  }
    76  
    77  func TestDiagdVerySlowlySent(t *testing.T) {
    78  	m := newDiagdMetadata(t)
    79  	m.check(0, 0, true, false)
    80  
    81  	// Don't send a snapshot, but advance the clock into the boot grace period.
    82  	// We should still be alive.
    83  	m.stepSec(300)
    84  	m.check(1, 300, true, false)
    85  
    86  	// Advance the clock to just before the end of the boot grace period.
    87  	// We should still be alive.
    88  	m.stepSec(299)
    89  	m.check(1, 599, true, false)
    90  
    91  	// Advance the clock to just past the end of the boot grace period.
    92  	// We should no longer be alive.
    93  	m.stepSec(1)
    94  	m.check(2, 600, false, false)
    95  
    96  	// Send a snapshot. We should stay dead.
    97  	m.stepSec(1)
    98  	m.dw.NoteSnapshotSent()
    99  	m.check(3, 601, false, false)
   100  
   101  	// Not very useful, but if we mark the snapshot processed, we should snap
   102  	// to alive and ready.
   103  	m.stepSec(1)
   104  	m.dw.NoteSnapshotProcessed()
   105  	m.check(2, 602, true, true)
   106  }
   107  
   108  func TestDiagdNeverProcessed(t *testing.T) {
   109  	m := newDiagdMetadata(t)
   110  	m.check(0, 0, true, false)
   111  
   112  	// Advance the clock 10s.
   113  	m.stepSec(10)
   114  
   115  	m.check(1, 10, true, false)
   116  
   117  	// Send a snapshot. We should still be alive but not ready.
   118  	m.dw.NoteSnapshotSent()
   119  	m.check(2, 10, true, false)
   120  
   121  	// Advance another 9m (540s).
   122  	m.stepSec(540)
   123  	m.check(3, 550, true, false)
   124  
   125  	// Advance the clock another minute. We're now past the ten-minute grace period.
   126  	m.stepSec(60)
   127  	m.check(4, 610, false, false)
   128  }
   129  
   130  func TestDiagdSlowlyProcessed(t *testing.T) {
   131  	m := newDiagdMetadata(t)
   132  	m.check(0, 0, true, false)
   133  
   134  	// Advance the clock 10s.
   135  	m.stepSec(10)
   136  	m.check(1, 10, true, false)
   137  
   138  	// Send a snapshot. We should still be alive but not ready.
   139  	m.dw.NoteSnapshotSent()
   140  	m.check(2, 10, true, false)
   141  
   142  	// Mark the snapshot processed after another 10s. This should take
   143  	// us to alive and ready.
   144  	m.stepSec(10)
   145  	m.dw.NoteSnapshotProcessed()
   146  	m.check(3, 20, true, true)
   147  
   148  	// Send another snapshot after 40s (to take us to an even minute).
   149  	// Still alive and ready.
   150  	m.stepSec(40)
   151  	m.dw.NoteSnapshotSent()
   152  	m.check(4, 60, true, true)
   153  
   154  	// Don't process that snapshot. 9m59s after sending, we should
   155  	// still be alive and ready.
   156  	m.stepSec(599)
   157  	m.check(5, 659, true, true)
   158  
   159  	// At 10m after sending, we should become not alive and not ready.
   160  	m.stepSec(1)
   161  	m.check(6, 660, false, false)
   162  }
   163  

View as plain text