aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/proto/server_hello_test.go
blob: 99f3462159c16b2600484963c6f8344a48ef36ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package proto

import (
	"bytes"
	"encoding/hex"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestServerHello_DecodeAware(t *testing.T) {
	data, err := hex.DecodeString("11436c69636b486f75736520736572766572150bb2a9030d4575726f70652f4d6f73636f7705616c70686103")
	require.NoError(t, err)

	r := NewReader(bytes.NewReader(data))
	var v ServerHello
	require.NoError(t, v.DecodeAware(r, Version))
	require.Equal(t, ServerHello{
		Name:        "ClickHouse server",
		Major:       21,
		Minor:       11,
		Patch:       3,
		Revision:    54450,
		Timezone:    "Europe/Moscow",
		DisplayName: "alpha",
	}, v)

	assert.True(t, v.Has(FeatureDisplayName))
	assert.False(t, v.Has(Feature(10_100_000)))
	features := []Feature{
		50264, 51903, 54058, 54060, 54372, 54401, 54406, 54410,
		54420, 54429, 54441, 54442, 54443, 54447, 54448, 54449,
	}
	assert.Equal(t, features, v.Features())
	assert.Equal(t, "ClickHouse server (alpha, Europe/Moscow) 21.11.3 (54450)", v.String())
}

func TestServerHello_EncodeAware(t *testing.T) {
	var b Buffer
	v := ServerHello{
		Name:        "ClickHouse server",
		Major:       21,
		Minor:       11,
		Patch:       3,
		Revision:    54450,
		Timezone:    "Europe/Moscow",
		DisplayName: "alpha",
	}
	Gold(t, &v)
	v.EncodeAware(&b, Version)
	t.Run("Decode", func(t *testing.T) {
		var dec ServerHello
		buf := skipCode(t, b.Buf, int(ServerCodeHello))
		requireDecode(t, buf, aware(&dec))
		require.Equal(t, v, dec)
		requireNoShortRead(t, buf, aware(&dec))
	})
}

func BenchmarkServerHello_Decode(b *testing.B) {
	var raw Buffer
	raw.PutString("ClickHouse server")
	raw.PutInt(21)
	raw.PutInt(11)
	raw.PutInt(54450)
	raw.PutString("Europe/Moscow")
	raw.PutString("alpha")
	raw.PutInt(3)

	b.Logf("%x", raw.Buf)

	buf := new(bytes.Reader)

	r := NewReader(buf)

	b.Run("Struct", func(b *testing.B) {
		b.SetBytes(int64(len(raw.Buf)))
		b.ReportAllocs()
		var serverHello ServerHello
		for i := 0; i < b.N; i++ {
			buf.Reset(raw.Buf)
			r.raw.Reset(buf)

			if err := serverHello.DecodeAware(r, Version); err != nil {
				b.Fatal(err)
			}
		}
	})
	b.Run("Raw", func(b *testing.B) {
		b.SetBytes(int64(len(raw.Buf)))
		b.ReportAllocs()
		for i := 0; i < b.N; i++ {
			buf.Reset(raw.Buf)
			r.raw.Reset(buf)

			name, err := r.StrRaw()
			if err != nil {
				b.Fatal(err)
			}

			major, err := r.Int()
			if err != nil {
				b.Fatal(err)
			}
			minor, err := r.Int()
			if err != nil {
				b.Fatal(err)
			}
			revision, err := r.Int()
			if err != nil {
				b.Fatal(err)
			}

			if FeatureTimezone.In(revision) {
				v, err := r.StrRaw()
				if err != nil {
					b.Fatal(err)
				}
				_ = v
			}
			if FeatureDisplayName.In(revision) {
				v, err := r.StrRaw()
				if err != nil {
					b.Fatal(err)
				}
				_ = v
			}
			if FeatureVersionPatch.In(revision) {
				v, err := r.Int()
				if err != nil {
					b.Fatal(err)
				}
				_ = v
			}

			_ = name
			_ = major
			_ = minor
			_ = revision
		}
	})
}