github.com/grahambrereton-form3/tilt@v0.10.18/internal/store/store_test.go (about)

     1  package store
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestProcessActions(t *testing.T) {
    11  	f := newFixture(t)
    12  	f.Start()
    13  
    14  	f.store.Dispatch(CompletedBuildAction{})
    15  	f.store.Dispatch(CompletedBuildAction{})
    16  	f.store.Dispatch(DoneAction{})
    17  
    18  	f.WaitUntilDone()
    19  
    20  	assert.Equal(t, 2, f.store.state.CompletedBuildCount)
    21  }
    22  
    23  func TestBroadcastActions(t *testing.T) {
    24  	f := newFixture(t)
    25  
    26  	s := newFakeSubscriber()
    27  	f.store.AddSubscriber(f.ctx, s)
    28  
    29  	f.Start()
    30  
    31  	f.store.Dispatch(CompletedBuildAction{})
    32  
    33  	s.assertOnChangeCount(t, 1)
    34  
    35  	f.store.Dispatch(DoneAction{})
    36  	f.WaitUntilDone()
    37  }
    38  
    39  func TestBroadcastActionsBatching(t *testing.T) {
    40  	f := newFixture(t)
    41  
    42  	s := newFakeSubscriber()
    43  	f.store.AddSubscriber(f.ctx, s)
    44  
    45  	f.Start()
    46  
    47  	f.store.mu.Lock()
    48  	f.store.Dispatch(CompletedBuildAction{})
    49  	f.store.Dispatch(CompletedBuildAction{})
    50  	f.store.mu.Unlock()
    51  
    52  	s.assertOnChangeCount(t, 1)
    53  
    54  	f.store.Dispatch(DoneAction{})
    55  	f.WaitUntilDone()
    56  }
    57  
    58  type fixture struct {
    59  	t      *testing.T
    60  	store  *Store
    61  	ctx    context.Context
    62  	cancel func()
    63  	done   chan error
    64  }
    65  
    66  func newFixture(t *testing.T) fixture {
    67  	ctx, cancel := context.WithCancel(context.Background())
    68  	st := NewStore(TestReducer, LogActionsFlag(false))
    69  	return fixture{
    70  		t:      t,
    71  		store:  st,
    72  		ctx:    ctx,
    73  		cancel: cancel,
    74  		done:   make(chan error),
    75  	}
    76  }
    77  
    78  func (f fixture) Start() {
    79  	go func() {
    80  		err := f.store.Loop(f.ctx)
    81  		f.done <- err
    82  	}()
    83  }
    84  
    85  func (f fixture) WaitUntilDone() {
    86  	err := <-f.done
    87  	if err != nil && err != context.Canceled {
    88  		f.t.Fatalf("Loop failed unexpectedly: %v", err)
    89  	}
    90  }
    91  
    92  func (f fixture) TearDown() {
    93  	f.cancel()
    94  	f.WaitUntilDone()
    95  }
    96  
    97  type CompletedBuildAction struct {
    98  }
    99  
   100  func (CompletedBuildAction) Action() {}
   101  
   102  type DoneAction struct {
   103  }
   104  
   105  func (DoneAction) Action() {}
   106  
   107  var TestReducer = Reducer(func(ctx context.Context, s *EngineState, action Action) {
   108  	switch action.(type) {
   109  	case CompletedBuildAction:
   110  		s.CompletedBuildCount++
   111  	case DoneAction:
   112  		s.FatalError = context.Canceled
   113  	}
   114  })