github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/topic/topicwriterinternal/writer_test.go (about)

     1  package topicwriterinternal
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	"go.uber.org/mock/gomock"
    10  )
    11  
    12  func TestWriterWaitInit(t *testing.T) {
    13  	t.Run("Ok", func(t *testing.T) {
    14  		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    15  		defer cancel()
    16  
    17  		mc := gomock.NewController(t)
    18  		defer mc.Finish()
    19  
    20  		strm := NewMockStreamWriter(mc)
    21  
    22  		strm.EXPECT().WaitInit(ctx).Times(2)
    23  
    24  		_, err := strm.WaitInit(ctx)
    25  		require.NoError(t, err)
    26  
    27  		// one more run is needed to check idempotency
    28  		_, err = strm.WaitInit(ctx)
    29  		require.NoError(t, err)
    30  	})
    31  }
    32  
    33  func TestWriterWrite(t *testing.T) {
    34  	t.Run("Ok", func(t *testing.T) {
    35  		ctx := context.Background()
    36  		t.Run("OK", func(t *testing.T) {
    37  			mc := gomock.NewController(t)
    38  
    39  			strm := NewMockStreamWriter(mc)
    40  			strm.EXPECT().Write(ctx, newTestMessages(1))
    41  			w := Writer{
    42  				streamWriter: strm,
    43  			}
    44  			require.NoError(t, w.Write(ctx, PublicMessage{SeqNo: 1}))
    45  		})
    46  	})
    47  }
    48  
    49  func TestWriterWriteMessage(t *testing.T) {
    50  	ctx := context.Background()
    51  	t.Run("OK", func(t *testing.T) {
    52  		mc := gomock.NewController(t)
    53  
    54  		strm := NewMockStreamWriter(mc)
    55  		strm.EXPECT().Write(ctx, newTestMessages(1, 3))
    56  		w := Writer{
    57  			streamWriter: strm,
    58  		}
    59  		require.NoError(t, w.Write(ctx, PublicMessage{SeqNo: 1}, PublicMessage{SeqNo: 3}))
    60  	})
    61  }