1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package managedwriter
16
17import (
18	"sync"
19	"testing"
20
21	"github.com/google/go-cmp/cmp"
22)
23
24func TestWriterOptions(t *testing.T) {
25
26	testCases := []struct {
27		desc    string
28		options []WriterOption
29		want    *ManagedStream
30	}{
31		{
32			desc:    "WithType",
33			options: []WriterOption{WithType(BufferedStream)},
34			want: func() *ManagedStream {
35				ms := &ManagedStream{
36					streamSettings: defaultStreamSettings(),
37				}
38				ms.streamSettings.streamType = BufferedStream
39				return ms
40			}(),
41		},
42		{
43			desc:    "WithMaxInflightRequests",
44			options: []WriterOption{WithMaxInflightRequests(2)},
45			want: func() *ManagedStream {
46				ms := &ManagedStream{
47					streamSettings: defaultStreamSettings(),
48				}
49				ms.streamSettings.MaxInflightRequests = 2
50				return ms
51			}(),
52		},
53		{
54			desc:    "WithMaxInflightBytes",
55			options: []WriterOption{WithMaxInflightBytes(5)},
56			want: func() *ManagedStream {
57				ms := &ManagedStream{
58					streamSettings: defaultStreamSettings(),
59				}
60				ms.streamSettings.MaxInflightBytes = 5
61				return ms
62			}(),
63		},
64		{
65			desc:    "WithTracePrefix",
66			options: []WriterOption{WithTraceID("foo")},
67			want: func() *ManagedStream {
68				ms := &ManagedStream{
69					streamSettings: defaultStreamSettings(),
70				}
71				ms.streamSettings.TraceID = "foo"
72				return ms
73			}(),
74		},
75		{
76			desc:    "WithDestinationTable",
77			options: []WriterOption{WithDestinationTable("foo")},
78			want: func() *ManagedStream {
79				ms := &ManagedStream{
80					streamSettings:   defaultStreamSettings(),
81					destinationTable: "foo",
82				}
83				return ms
84			}(),
85		},
86		{
87			desc:    "WithDataOrigin",
88			options: []WriterOption{WithDataOrigin("origin")},
89			want: func() *ManagedStream {
90				ms := &ManagedStream{
91					streamSettings: defaultStreamSettings(),
92				}
93				ms.streamSettings.dataOrigin = "origin"
94				return ms
95			}(),
96		},
97		{
98			desc: "multiple",
99			options: []WriterOption{
100				WithType(PendingStream),
101				WithMaxInflightBytes(5),
102				WithTraceID("id"),
103			},
104			want: func() *ManagedStream {
105				ms := &ManagedStream{
106					streamSettings: defaultStreamSettings(),
107				}
108				ms.streamSettings.MaxInflightBytes = 5
109				ms.streamSettings.streamType = PendingStream
110				ms.streamSettings.TraceID = "id"
111				return ms
112			}(),
113		},
114	}
115
116	for _, tc := range testCases {
117		got := &ManagedStream{
118			streamSettings: defaultStreamSettings(),
119		}
120		for _, o := range tc.options {
121			o(got)
122		}
123
124		if diff := cmp.Diff(got, tc.want,
125			cmp.AllowUnexported(ManagedStream{}, streamSettings{}),
126			cmp.AllowUnexported(sync.Mutex{})); diff != "" {
127			t.Errorf("diff in case (%s):\n%v", tc.desc, diff)
128		}
129	}
130}
131