bind.tap.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict';
  2. // stdlib
  3. var tap = require('tap');
  4. var test = tap.test;
  5. var EventEmitter = require('events').EventEmitter;
  6. // module under test
  7. var context = require('../context.js');
  8. // multiple contexts in use
  9. var tracer = context.createNamespace('tracer');
  10. function Trace(harvester) {
  11. this.harvester = harvester;
  12. }
  13. Trace.prototype.runHandler = function (callback) {
  14. var wrapped = tracer.bind(function () {
  15. callback();
  16. this.harvester.emit('finished', tracer.get('transaction'));
  17. }.bind(this));
  18. wrapped();
  19. };
  20. test("simple tracer built on contexts", function (t) {
  21. t.plan(6);
  22. var harvester = new EventEmitter();
  23. var trace = new Trace(harvester);
  24. harvester.on('finished', function (transaction) {
  25. t.ok(transaction, "transaction should have been passed in");
  26. t.equal(transaction.status, 'ok', "transaction should have finished OK");
  27. t.equal(Object.keys(process.namespaces).length, 1, "Should only have one namespace.");
  28. });
  29. trace.runHandler(function inScope() {
  30. t.ok(tracer.active, "tracer should have an active context");
  31. tracer.set('transaction', {status : 'ok'});
  32. t.ok(tracer.get('transaction'), "can retrieve newly-set value");
  33. t.equal(tracer.get('transaction').status, 'ok', "value should be correct");
  34. });
  35. });