forked from v3io/v3io-go-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
146 lines (110 loc) · 3.79 KB
/
context_test.go
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
package v3io
import (
"fmt"
"testing"
"github.com/nuclio/logger"
"github.com/nuclio/zap"
"github.com/stretchr/testify/suite"
)
//
// Base
//
type ContextTestSuite struct {
suite.Suite
logger logger.Logger
context *Context
session *Session
container *Container
}
func (suite *ContextTestSuite) SetupTest() {
var err error
suite.logger, err = nucliozap.NewNuclioZapTest("test")
suite.context, err = NewContext(suite.logger, "<value>", 8)
suite.Require().NoError(err, "Failed to create context")
suite.session, err = suite.context.NewSession("iguazio", "<value>", "")
suite.Require().NoError(err, "Failed to create session")
suite.container, err = suite.session.NewContainer("1024")
suite.Require().NoError(err, "Failed to create container")
}
func (suite *ContextTestSuite) TestObject() {
numRequests := 10
pathFormat := "object-%d.txt"
contentsFormat := "contents: %d"
someContext := 30
responseChan := make(chan *Response, 128)
//
// Put 10 objects
//
// submit 10 put object requests asynchronously
for requestIndex := 0; requestIndex < numRequests; requestIndex++ {
request, err := suite.container.PutObject(&PutObjectInput{
Path: fmt.Sprintf(pathFormat, requestIndex),
Body: []byte(fmt.Sprintf(contentsFormat, requestIndex)),
}, &someContext, responseChan)
suite.Require().NoError(err)
suite.Require().NotNil(request)
}
// wait on response channel for responses
suite.waitForResponses(responseChan, numRequests, nil)
//
// Get the 10 object's contents
//
pendingGetRequests := map[uint64]int{}
// submit 10 get object requests
for requestIndex := 0; requestIndex < numRequests; requestIndex++ {
request, err := suite.container.GetObject(&GetObjectInput{
Path: fmt.Sprintf(pathFormat, requestIndex),
}, &someContext, responseChan)
suite.Require().NoError(err)
suite.Require().NotNil(request)
// store the request ID and the request index so that we can compare
// the response contents
pendingGetRequests[request.ID] = requestIndex
}
// wait for the responses and verify the contents
suite.waitForResponses(responseChan, numRequests, func(response *Response) {
pendingRequestIndex := pendingGetRequests[response.ID]
// verify the context
suite.Require().Equal(&someContext, response.Context)
// verify that the body of the response is equal to the contents formatting with the request index
// as mapped from the response ID
suite.Require().Equal(fmt.Sprintf(contentsFormat, pendingRequestIndex), string(response.Body()))
// get the request input
getObjectInput := response.requestResponse.Request.Input.(*GetObjectInput)
// verify that the request path is correct
suite.Require().Equal(fmt.Sprintf(pathFormat, pendingRequestIndex), getObjectInput.Path)
})
//
// Delete the objects
//
// submit 10 delete object requests asynchronously
for requestIndex := 0; requestIndex < numRequests; requestIndex++ {
request, err := suite.container.DeleteObject(&DeleteObjectInput{
Path: fmt.Sprintf(pathFormat, requestIndex),
}, &someContext, responseChan)
suite.Require().NoError(err)
suite.Require().NotNil(request)
}
// wait on response channel for responses
suite.waitForResponses(responseChan, numRequests, nil)
}
func (suite *ContextTestSuite) waitForResponses(responseChan chan *Response, numResponses int, verifier func(*Response)) {
for numResponses != 0 {
// read a response
response := <-responseChan
// verify there's no error
suite.Require().NoError(response.Error)
// one less left to wait for
numResponses--
if verifier != nil {
verifier(response)
}
// release the response
response.Release()
}
}
// In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestContextTestSuite(t *testing.T) {
suite.Run(t, new(ContextTestSuite))
}