File: tilt_compilesite_test.rb

package info (click to toggle)
ruby-tilt 2.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 648 kB
  • sloc: ruby: 4,998; makefile: 7
file content (45 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require_relative 'test_helper'

describe 'tilt compile site' do
  before do
    GC.start
  end

  _CompilingTemplate = Class.new(Tilt::Template) do
    def precompiled_template(locals)
      @data.inspect
    end
  end

  _Scope = Class.new

  it "compiling template source to a method" do
    template = _CompilingTemplate.new { |t| "Hello World!" }
    template.render(_Scope.new)
    method = template.send(:compiled_method, [])
    assert_kind_of UnboundMethod, method
  end

  # This it attempts to surface issues with compiling templates from
  # multiple threads.
  it "using compiled templates from multiple threads" do
    template = _CompilingTemplate.new { 'template' }
    main_thread = Thread.current
    10.times do |i|
      threads =
        (1..50).map do |j|
          Thread.new {
            begin
              locals = { "local#{i}" => 'value' }
              template.render(self, locals)
              thread_id = Thread.current.object_id
              template.render(self, "local#{thread_id.abs.to_s}" => 'value')
            rescue => boom
              main_thread.raise(boom)
            end
          }
        end
      threads.each { |t| t.join }
    end
  end
end