Find way to test Snowplow event tracking without creating new thread
The following discussion from !39725 (merged) should be addressed:
-
@rpereira2 started a discussion: (+2 comments) I wonder if this will cause actual event data to be generated, and if that can cause problems?
🤔
Snowplow does some runtime type-checking, which can cause runtime exceptions on production (ex: #238229 (closed)). When running rspec tests, snowplow is disabled by default, so none of the runtime type checking is executed.
In order to execute Snowplow code, we can enable snowplow and set a hostname with stub_application_setting(snowplow_enabled: true, snowplow_collector_hostname: 'localhost')
.
However, this results in an SnowplowTracker::AsyncEmitter
being created. This will attempt to send data to a hostname that doesn't exist.
Some alternatives, suggested by @ali-gitlab:
I suspect that this did not fire a request because the test finished too soon:
A note on testing: if you test the AsyncEmitter by using a short script to send an event, you may find that the event fails to send. This is because the process exits before the flushing thread is finished. You can get round this either by adding a sleep(10) to the end of your script or by using the synchronous flush.
In any case, it would attempt to send to an endpoint that doesn't exist (due to the hostname).
The
AsyncEmitter
creates a newThread
upon initialization, which may not be something we want to happen in tests.A couple of alternative ideas (not blocking this MR):
- We could use the synchronous
Emitter
when running tests, and stub the request that it makes- We could create a new
Emitter
that does not send any request, and use that when running tests