Sha256: 8a816c98b9497307575104a3d2f6af2016bac69fee0cd33acaa59935cd189411

Contents?: true

Size: 1.75 KB

Versions: 25

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require_relative 'helper'
require 'polyphony/adapters/trace'

class TraceTest < MiniTest::Test
  def test_tracing_disabled
    records = []
    t = Polyphony::Trace.new { |r| records << r if r[:event] =~ /^fiber_/ }
    t.enable
    snooze
    assert_equal 0, records.size
  ensure
    t&.disable
    Polyphony.trace(nil)
  end

  def test_tracing_enabled
    records = []
    t = Polyphony::Trace.new(:fiber_all) { |r| records << r if r[:event] =~ /^fiber_/ }
    Polyphony.trace(true)
    t.enable
    snooze
    t.disable
    
    assert_equal 3, records.size
    events = records.map { |r| r[:event] }
    assert_equal [:fiber_schedule, :fiber_switchpoint, :fiber_run], events
    assert_equal [Fiber.current], records.map { |r| r[:fiber] }.uniq
  ensure
    t&.disable
    Polyphony.trace(nil)
  end

  def test_2_fiber_trace
    records = []
    thread = Thread.current
    t = Polyphony::Trace.new(:fiber_all) do |r|
      records << r if Thread.current == thread && r[:event] =~ /^fiber_/
    end
    t.enable
    Polyphony.trace(true)

    f = spin { sleep 0 }
    suspend
    sleep 0

    events = records.map { |r| [r[:fiber] == f ? :f : :current, r[:event]] }
    assert_equal [
      [:f, :fiber_create],
      [:f, :fiber_schedule],
      [:current, :fiber_switchpoint],
      [:f, :fiber_run],
      [:f, :fiber_switchpoint],
      [:f, :fiber_event_poll_enter],
      [:f, :fiber_schedule],
      [:f, :fiber_event_poll_leave],
      [:f, :fiber_run],
      [:f, :fiber_terminate],
      [:current, :fiber_switchpoint],
      [:current, :fiber_event_poll_enter],
      [:current, :fiber_schedule],
      [:current, :fiber_event_poll_leave],
      [:current, :fiber_run]
    ], events
  ensure
    t&.disable
    Polyphony.trace(nil)
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
polyphony-0.58 test/test_trace.rb
polyphony-0.57.0 test/test_trace.rb
polyphony-0.56.0 test/test_trace.rb
polyphony-0.55.0 test/test_trace.rb
polyphony-0.54.0 test/test_trace.rb
polyphony-0.53.2 test/test_trace.rb
polyphony-0.53.1 test/test_trace.rb
polyphony-0.53.0 test/test_trace.rb
polyphony-0.52.0 test/test_trace.rb
polyphony-0.51.0 test/test_trace.rb
polyphony-0.50.1 test/test_trace.rb
polyphony-0.50.0 test/test_trace.rb
polyphony-0.49.2 test/test_trace.rb
polyphony-0.49.1 test/test_trace.rb
polyphony-0.49.0 test/test_trace.rb
polyphony-0.48.0 test/test_trace.rb
polyphony-0.47.5.1 test/test_trace.rb
polyphony-0.47.5 test/test_trace.rb
polyphony-0.47.4 test/test_trace.rb
polyphony-0.47.3 test/test_trace.rb