File: lazy_method_map_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (57 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (4)
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
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Execution::Lazy::LazyMethodMap do
  def self.test_lazy_method_map
    it "handles multithreaded access" do
      a = Class.new
      b = Class.new(a)
      c = Class.new(b)
      lazy_method_map.set(a, :a)
      threads = 1000.times.map do |i|
        Thread.new {
          d = Class.new(c)
          assert_equal :a, lazy_method_map.get(d.new)
        }
      end
      threads.map(&:join)
    end

    it "dups" do
      a = Class.new
      b = Class.new(a)
      c = Class.new(b)
      lazy_method_map.set(a, :a)
      lazy_method_map.get(b.new)
      lazy_method_map.get(c.new)

      dup_map = lazy_method_map.dup
      assert_equal 3, dup_map.instance_variable_get(:@storage).size
      assert_equal :a, dup_map.get(a.new)
      assert_equal :a, dup_map.get(b.new)
      assert_equal :a, dup_map.get(c.new)
    end
  end

  describe "with a plain hash" do
    let(:lazy_method_map) { GraphQL::Execution::Lazy::LazyMethodMap.new(use_concurrent: false) }
    test_lazy_method_map

    it "has a Ruby Hash inside" do
      storage = lazy_method_map
        .instance_variable_get(:@storage)
        .instance_variable_get(:@storage)
      assert_instance_of Hash, storage
    end
  end

  describe "with a Concurrent::Map" do
    let(:lazy_method_map) { GraphQL::Execution::Lazy::LazyMethodMap.new(use_concurrent: true) }
    test_lazy_method_map

    it "has a Concurrent::Map inside" do
      storage = lazy_method_map.instance_variable_get(:@storage)
      assert_instance_of Concurrent::Map, storage
    end
  end
end