-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_test.go
More file actions
165 lines (127 loc) · 4.21 KB
/
example_test.go
File metadata and controls
165 lines (127 loc) · 4.21 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package cassandra
import (
"context"
impulse_ctx "github.com/motiv-labs/impulse-ctx"
jaegermod "github.com/motiv-labs/jaeger"
log "github.com/motiv-labs/logwrapper"
"github.com/opentracing/opentracing-go"
"time"
)
func NewMockCtx() context.Context {
span := getSpan()
return context.WithValue(context.Background(), impulse_ctx.ImpulseCtxKey, impulse_ctx.ImpulseCtx{
Organization: "",
Span: span,
TraceId: "",
})
}
// getSpan simply retuns a real span to show passed in the Kafka examples.
func getSpan() opentracing.Span {
tracer, closer := jaegermod.Initialize("sync-manager")
defer closer.Close()
return tracer.StartSpan("example span")
}
func Example() {
ctx := NewMockCtx()
impulseCtx := ctx.Value(impulse_ctx.ImpulseCtxKey).(impulse_ctx.ImpulseCtx)
// Cassandra initialization - initializes Cassandra keyspace and creates tables if required
// Needs to be called only on the app startup
Initialize("system_keyspace", "application_keyspace", 120*time.Second, ctx)
// Now that Cassandra is initialized we can start new connections
// Getting a cassandra connection initializer
sessionInitializer := New("application_keyspace", ctx)
// Starting a new cassandra session
sessionHolder, err := sessionInitializer.NewSession(ctx)
if err != nil {
log.Errorf(impulseCtx, "error initializing cassandra session - %v", err)
return
}
defer sessionHolder.CloseSession(ctx)
// Getting the cassandra session
session := sessionHolder.GetSession(ctx)
// And have fun with the session, example:
session.Query(ctx, "SELECT * FROM my_table")
}
func ExampleNew() {
ctx := NewMockCtx()
impulseCtx := ctx.Value(impulse_ctx.ImpulseCtxKey).(impulse_ctx.ImpulseCtx)
// Getting a cassandra connection initializer
sessionInitializer := New("application_keyspace", ctx)
// Starting a new cassandra session
sessionHolder, err := sessionInitializer.NewSession(ctx)
if err != nil {
log.Errorf(impulseCtx, "error initializing cassandra session - %v", err)
return
}
defer sessionHolder.CloseSession(ctx)
// Getting the cassandra session
session := sessionHolder.GetSession(ctx)
// And have fun with the session, example:
session.Query(ctx, "SELECT * FROM my_table").Exec(ctx)
}
func Example_iterRetry_Scan() {
type Data struct {
Data1 string
Data2 string
Data3 string
Data4 string
}
ctx := NewMockCtx()
impulseCtx := ctx.Value(impulse_ctx.ImpulseCtxKey).(impulse_ctx.ImpulseCtx)
// Getting a cassandra connection initializer
sessionInitializer := New("application_keyspace", ctx)
// Starting a new cassandra session
sessionHolder, err := sessionInitializer.NewSession(ctx)
if err != nil {
log.Errorf(impulseCtx, "error initializing cassandra session - %v", err)
return
}
defer sessionHolder.CloseSession(ctx)
// Getting the cassandra session
session := sessionHolder.GetSession(ctx)
// And have fun with the session, example:
iter := session.Query(ctx, "SELECT * FROM my_table").Iter(ctx)
var data Data
var dataList []Data
for iter.Scan(ctx, &data.Data1, &data.Data2, &data.Data3, &data.Data4) {
dataList = append(dataList, data)
}
if err := iter.Close(ctx); err != nil {
log.Error(impulseCtx, "error while querying table")
// return ...
}
// return ...
}
func Example_iterRetry_ScanAndClose() {
type Data struct {
Data1 string
Data2 string
Data3 string
Data4 string
}
ctx := NewMockCtx()
impulseCtx := ctx.Value(impulse_ctx.ImpulseCtxKey).(impulse_ctx.ImpulseCtx)
// Getting a cassandra connection initializer
sessionInitializer := New("application_keyspace", ctx)
// Starting a new cassandra session
sessionHolder, err := sessionInitializer.NewSession(ctx)
if err != nil {
log.Errorf(impulseCtx, "error initializing cassandra session - %v", err)
return
}
defer sessionHolder.CloseSession(ctx)
// Getting the cassandra session
session := sessionHolder.GetSession(ctx)
// And have fun with the session, example:
iter := session.Query(ctx, "SELECT * FROM my_table").Iter(ctx)
var data Data
var dataList []Data
if err := iter.ScanAndClose(ctx, func() bool {
dataList = append(dataList, data)
return true
}, &data.Data1, &data.Data2, &data.Data3, &data.Data4); err != nil {
log.Error(impulseCtx, "error while querying table")
// return ...
}
// return ...
}