This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
GrpcExample.scala
146 lines (139 loc) · 5.04 KB
/
GrpcExample.scala
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 com.github.phisgr.example
import com.github.phisgr.example.chat._
import com.github.phisgr.example.util._
import com.github.phisgr.gatling.grpc.Predef._
import com.github.phisgr.gatling.pb._
import io.gatling.core.Predef._
import io.gatling.core.session.Expression
import io.grpc.Status
class GrpcExample extends Simulation {
TestServer.startServer()
TestServer.startEmptyServer()
val grpcConf = grpc(managedChannelBuilder(name = "localhost", port = 8080).usePlaintext())
.warmUpCall(ChatServiceGrpc.METHOD_GREET, GreetRequest.defaultInstance)
.header(TokenHeaderKey, optional = true)($("token"))
val emptyGrpc = grpc(managedChannelBuilder(name = "localhost", port = 9999).usePlaintext())
val greetPayload: Expression[GreetRequest] = GreetRequest(name = "World").updateExpr(
_.username :~ $("username")
)
val successfulCall = grpc("Success")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(greetPayload)
.extract(_.data.split(' ').headOption)(_ saveAs "s")
val s = scenario("Example")
.feed(csv("usernames.csv")) // the first two are duplicated to force an ALREADY_EXISTS error
.exec(
grpc("Expect UNAUTHENTICATED")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(greetPayload)
.check(
statusCode is Status.Code.UNAUTHENTICATED,
statusDescription.notExists,
trailer(ErrorResponseKey) is CustomError("You are not authenticated!"),
trailer(ErrorResponseKey).find(1).notExists,
trailer(ErrorResponseKey).findAll is List(CustomError("You are not authenticated!")),
trailer(ErrorResponseKey).count is 1,
trailer(TokenHeaderKey).count is 0
)
)
.exec(
grpc("Register")
.rpc(ChatServiceGrpc.METHOD_REGISTER)
.payload(RegisterRequest.defaultInstance.updateExpr(
_.username :~ $("username")
))
.extract(_.token.some)(_ saveAs "token")
)
.exitHereIfFailed
.exec(successfulCall)
.exec(
grpc("Empty server")
.rpc(ChatServiceGrpc.METHOD_REGISTER)
.payload(RegisterRequest.defaultInstance)
// only one error
.checkIf(_.userId == 10)(statusCode is Status.Code.ABORTED)
.checkIf(_.userId != 10)(statusCode is Status.Code.UNIMPLEMENTED)
.target(emptyGrpc)
)
.exec(
grpc("Cannot Build")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload($("notDefined"))
.check(
// the extraction checks can be added inside .check
// but the extraction functions need type annotation
// so .extract and .extractMultiple are added to GrpcCallActionBuilder, see below
extract((_: ChatMessage).username.some),
extractMultiple((_: ChatMessage).data.split(' ').toSeq.some)
)
)
.exec(
grpc("Cannot Build, with header not found")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(GreetRequest.defaultInstance)
.header(TokenHeaderKey)($("notDefined"))
)
.exec(
grpc("Trigger logging")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(greetPayload)
.check(statusCode is Status.Code.NOT_FOUND)
)
.repeat(1000) {
exec(successfulCall)
.exec(
grpc("Expect PERMISSION_DENIED")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(GreetRequest(username = "DoesNotExist"))
.checkIf((res, _) => res.validation.toOption.isEmpty)(statusCode is Status.Code.PERMISSION_DENIED)
)
.exec(
grpc("Use session")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(
GreetRequest.defaultInstance.updateExpr(
_.name :~ $("s"),
_.username :~ $("username")
)
)
.extract(_.data.some)(_ is "Server says: Hello Server!")
)
.exec(
grpc("Extract multiple")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(greetPayload)
.extractMultipleIf(_.userId <= 10)(_.data.split(' ').toSeq.some)(
_.count is 4,
_.find(10).notExists,
_.find(2) is "Hello",
_.findAll is List("Server", "says:", "Hello", "World!")
)
.extractMultipleIf((message, _) =>
!message.status.isOk
)(_.username.toSeq.some
)( // this check should fail, but condition is not met, so no failure
_.find(1) is 'u'
)
)
}
.exec(
grpc("Extraction crash")
.rpc(ChatServiceGrpc.METHOD_GREET)
.payload(greetPayload)
.extractIf(
_.userId == 50)( // only 1 error here
_.data.split(' ')(10).some)( // This will crash, see _.find(10) above
_.exists
)
.extractIf { (res, session) =>
session.userId == 51 && // only 1 error here
res.validation.toOption.get.data.startsWith("Server") // always true
}(
_.data.split(' ')(10).some)(
_.exists
)
)
setUp(
s.inject(atOnceUsers(54))
).protocols(grpcConf)
}