- Notifications
You must be signed in to change notification settings - Fork 314
Add deeper tests for DDApi using ratpack as a mock http server. #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions 23 dd-trace/src/test/groovy/com/datadog/trace/SpanFactory.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.datadog.trace | ||
| ||
import com.datadoghq.trace.DDSpan | ||
import com.datadoghq.trace.DDSpanContext | ||
| ||
class SpanFactory { | ||
static def newSpanOf(long timestampMicro) { | ||
def context = new DDSpanContext( | ||
1L, | ||
1L, | ||
0L, | ||
"fakeService", | ||
"fakeOperation", | ||
"fakeResource", | ||
Collections.emptyMap(), | ||
false, | ||
"fakeType", | ||
Collections.emptyMap(), | ||
null, | ||
null); | ||
return new DDSpan(timestampMicro, context) | ||
} | ||
} |
117 changes: 117 additions & 0 deletions 117 dd-trace/src/test/groovy/com/datadoghq/trace/writer/DDApiTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.datadoghq.trace.writer | ||
| ||
import com.datadog.trace.SpanFactory | ||
import com.datadoghq.trace.DDSpan | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import ratpack.http.MediaType | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Specification | ||
| ||
import java.util.concurrent.atomic.AtomicReference | ||
| ||
import static ratpack.groovy.test.embed.GroovyEmbeddedApp.ratpack | ||
import static ratpack.http.MediaType.APPLICATION_JSON | ||
| ||
class DDApiTest extends Specification { | ||
static def mapper = new ObjectMapper() | ||
| ||
def "sending an empty list of traces returns no errors"() { | ||
setup: | ||
def agent = ratpack { | ||
handlers { | ||
put("v0.3/traces") { | ||
response.status(200).send() | ||
} | ||
} | ||
} | ||
def client = new DDApi("localhost", agent.address.port) | ||
| ||
expect: | ||
client.sendTraces([]) | ||
| ||
cleanup: | ||
agent.close() | ||
} | ||
| ||
def "non-200 response results in false returned"() { | ||
setup: | ||
def agent = ratpack { | ||
handlers { | ||
put("v0.3/traces") { | ||
response.status(404).send() | ||
} | ||
} | ||
} | ||
def client = new DDApi("localhost", agent.address.port) | ||
| ||
expect: | ||
!client.sendTraces([]) | ||
| ||
cleanup: | ||
agent.close() | ||
} | ||
| ||
def "content is sent as JSON"() { | ||
setup: | ||
def requestContentType = new AtomicReference<MediaType>() | ||
def requestBody = new AtomicReference<String>() | ||
def agent = ratpack { | ||
handlers { | ||
put("v0.3/traces") { | ||
requestContentType.set(request.contentType) | ||
request.body.then { | ||
requestBody.set(it.text) | ||
response.send() | ||
} | ||
} | ||
} | ||
} | ||
def client = new DDApi("localhost", agent.address.port) | ||
| ||
expect: | ||
client.sendTraces(traces) | ||
requestContentType.get().type == APPLICATION_JSON | ||
areEqual(requestBody.get(), expectedRequestBody) | ||
| ||
cleanup: | ||
agent.close() | ||
| ||
where: | ||
traces | expectedRequestBody | ||
[] | '[]' | ||
[SpanFactory.newSpanOf(1L)] | '''[{ | ||
"duration":0, | ||
"error":0, | ||
"meta":{"thread-name":"main","thread-id":"1"}, | ||
"name":"fakeOperation", | ||
"parent_id":0, | ||
"resource":"fakeResource" | ||
"service":"fakeService", | ||
"span_id":1, | ||
"start":1000, | ||
"trace_id":1, | ||
"type":"fakeType", | ||
}]''' | ||
[SpanFactory.newSpanOf(100L)] | '''[{ | ||
"duration":0, | ||
"error":0, | ||
"meta":{"thread-name":"main","thread-id":"1"}, | ||
"name":"fakeOperation", | ||
"parent_id":0, | ||
"resource":"fakeResource" | ||
"service":"fakeService", | ||
"span_id":1, | ||
"start":100000, | ||
"trace_id":1, | ||
"type":"fakeType", | ||
}]''' | ||
} | ||
| ||
| ||
static void areEqual(String json1, String json2) { | ||
def tree1 = mapper.readTree json1 | ||
def tree2 = mapper.readTree json2 | ||
| ||
assert tree1.equals(tree2) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitively a great idea :)