Improve reliability of tests (#643)

* Run tests on Windows in Github Actions

* core sha update

* format code

* fix ci yaml

* rebase

* lint

* Try without win+py3.6 fix

* Try without win+py3.6 fix

* Improve test reliability

Update some tests to use more deterministic methods of testing in memory
spans. This helps the core repo pass tests after adding Windows to CI
matrix.
This commit is contained in:
Owais Lone
2021-09-01 15:36:13 +05:30
committed by GitHub
parent c279ee59a8
commit cc57aacd71
6 changed files with 61 additions and 46 deletions

View File

@ -60,19 +60,13 @@ class TestElasticsearchIntegration(TestBase):
with self.disable_logging():
ElasticsearchInstrumentor().uninstrument()
def get_ordered_finished_spans(self):
return sorted(
self.memory_exporter.get_finished_spans(),
key=lambda s: s.start_time,
)
def test_instrumentor(self, request_mock):
request_mock.return_value = (1, {}, {})
es = Elasticsearch()
es.index(index="sw", doc_type="people", id=1, body={"name": "adam"})
spans_list = self.get_ordered_finished_spans()
spans_list = self.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
@ -87,7 +81,7 @@ class TestElasticsearchIntegration(TestBase):
es.index(index="sw", doc_type="people", id=1, body={"name": "adam"})
spans_list = self.get_ordered_finished_spans()
spans_list = self.get_finished_spans()
self.assertEqual(len(spans_list), 1)
def test_span_not_recording(self, request_mock):
@ -127,7 +121,7 @@ class TestElasticsearchIntegration(TestBase):
es = Elasticsearch()
es.index(index="sw", doc_type="people", id=1, body={"name": "adam"})
spans_list = self.get_ordered_finished_spans()
spans_list = self.get_finished_spans()
self.assertEqual(len(spans_list), 1)
span = spans_list[0]
self.assertTrue(span.name.startswith(prefix))
@ -141,7 +135,7 @@ class TestElasticsearchIntegration(TestBase):
es = Elasticsearch()
es.get(index="test-index", doc_type="tweet", id=1)
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
self.assertEqual(1, len(spans))
self.assertEqual("False", spans[0].attributes["elasticsearch.found"])
@ -169,7 +163,7 @@ class TestElasticsearchIntegration(TestBase):
except Exception: # pylint: disable=broad-except
pass
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
self.assertEqual(1, len(spans))
span = spans[0]
self.assertFalse(span.status.is_ok)
@ -186,13 +180,13 @@ class TestElasticsearchIntegration(TestBase):
index="sw", doc_type="people", id=1, body={"name": "adam"}
)
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
self.assertEqual(len(spans), 2)
self.assertEqual(spans[0].name, "parent")
self.assertEqual(spans[1].name, "Elasticsearch/sw/people/1")
self.assertIsNotNone(spans[1].parent)
self.assertEqual(spans[1].parent.span_id, spans[0].context.span_id)
parent = spans.by_name("parent")
child = spans.by_name("Elasticsearch/sw/people/1")
self.assertIsNotNone(child.parent)
self.assertEqual(child.parent.span_id, parent.context.span_id)
def test_multithread(self, request_mock):
request_mock.return_value = (1, {}, {})
@ -222,16 +216,15 @@ class TestElasticsearchIntegration(TestBase):
t1.join()
t2.join()
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
self.assertEqual(3, len(spans))
s1, s2, s3 = spans
self.assertEqual(s1.name, "parent")
s1 = spans.by_name("parent")
s2 = spans.by_name("Elasticsearch/test-index/tweet/1")
s3 = spans.by_name("Elasticsearch/test-index/tweet/2")
self.assertEqual(s2.name, "Elasticsearch/test-index/tweet/1")
self.assertIsNotNone(s2.parent)
self.assertEqual(s2.parent.span_id, s1.context.span_id)
self.assertEqual(s3.name, "Elasticsearch/test-index/tweet/2")
self.assertIsNone(s3.parent)
def test_dsl_search(self, request_mock):
@ -242,7 +235,7 @@ class TestElasticsearchIntegration(TestBase):
"term", author="testing"
)
search.execute()
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
span = spans[0]
self.assertEqual(1, len(spans))
self.assertEqual(span.name, "Elasticsearch/test-index/_search")
@ -270,10 +263,11 @@ class TestElasticsearchIntegration(TestBase):
client = Elasticsearch()
Article.init(using=client)
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
self.assertEqual(2, len(spans))
span1, span2 = spans
self.assertEqual(span1.name, "Elasticsearch/test-index")
span1 = spans.by_attr(key="elasticsearch.method", value="HEAD")
span2 = spans.by_attr(key="elasticsearch.method", value="PUT")
self.assertEqual(
span1.attributes,
{
@ -283,7 +277,6 @@ class TestElasticsearchIntegration(TestBase):
},
)
self.assertEqual(span2.name, "Elasticsearch/test-index")
attributes = {
SpanAttributes.DB_SYSTEM: "elasticsearch",
"elasticsearch.url": "/test-index",
@ -306,7 +299,7 @@ class TestElasticsearchIntegration(TestBase):
)
res = article.save(using=client)
self.assertTrue(res)
spans = self.get_ordered_finished_spans()
spans = self.get_finished_spans()
self.assertEqual(1, len(spans))
span = spans[0]
self.assertEqual(span.name, helpers.dsl_index_span_name)