rubyuw 0.99.9 → 0.99.10

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.99.9
1
+ 0.99.10
data/lib/rubyuw/base.rb CHANGED
@@ -14,6 +14,9 @@ module RubyUW
14
14
  # RubyUW::Base.authenticate("uwnetid", "password")
15
15
  class Base
16
16
  @@connection = nil
17
+
18
+ # See allow_concurrency=
19
+ @@allow_concurrency = false
17
20
 
18
21
  class <<self
19
22
  # Authenticate with MyUW. This method sets up and verifies
@@ -68,13 +71,36 @@ module RubyUW
68
71
  connection.cookie_jar.clear!
69
72
  end
70
73
 
74
+ # Determines whether RubyUW will be thread-safe. If set to true,
75
+ # each thread will have its own individual connection object.
76
+ # This also means that each thread is expected to authenticate
77
+ # with MyUW if necessary.
78
+ #
79
+ # If false, the connection object will be shared through a global
80
+ # class variable.
81
+ #
82
+ # Warning: This will reset all connections when the value changes.
83
+ # Therefore, if you're calling this sometime after using the connection
84
+ # already (which you shouldn't), you'll have to reauthenticate.
85
+ def allow_concurrency=(value)
86
+ return if @@allow_concurrency == !!value
87
+
88
+ # Reset all connections so we don't have any dangling
89
+ reset_connection!
90
+
91
+ # Forcing boolean value
92
+ @@allow_concurrency = !!value
93
+ end
94
+
95
+ def allow_concurrency; @@allow_concurrency; end
96
+
71
97
  # Returns the connection object. Used by other portions of RubyUW
72
98
  # to browse the pages, authenticate, and more. This method will
73
99
  # typically *never* be called by non-internal systems.
74
100
  #
75
101
  # @return [WWW::Mechanize] A WWW::Mechanize object.
76
102
  def connection
77
- @@connection ||= RubyUW::Connection.new do |browser|
103
+ connection_thread[:connection] ||= RubyUW::Connection.new do |browser|
78
104
  browser.user_agent_alias = 'Mac Safari'
79
105
  browser.follow_meta_refresh = true
80
106
 
@@ -95,7 +121,7 @@ module RubyUW
95
121
  # Resets the connection by clearing out the old. This way,
96
122
  # when connection is next called, it will recreate the object.
97
123
  def reset_connection!
98
- @@connection = nil
124
+ connection_thread[:connection] = nil
99
125
  self
100
126
  end
101
127
 
@@ -117,6 +143,13 @@ module RubyUW
117
143
 
118
144
  data
119
145
  end
146
+
147
+ protected
148
+
149
+ # Returns the thread on which to store the connections
150
+ def connection_thread
151
+ allow_concurrency ? Thread.current : Thread.main
152
+ end
120
153
  end
121
154
  end
122
155
  end
data/rubyuw.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubyuw}
8
- s.version = "0.99.9"
8
+ s.version = "0.99.10"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mitchell Hashimoto"]
@@ -1,6 +1,79 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class BaseTest < Test::Unit::TestCase
4
+ setup do
5
+ RubyUW::Base.publicize_singleton_methods
6
+ end
7
+
8
+ teardown do
9
+ RubyUW::Base.protect_singleton_methods
10
+ end
11
+
12
+ context "multithreading" do
13
+ setup do
14
+ RubyUW::Base.reset_connection!
15
+ RubyUW::Base.allow_concurrency = false
16
+ end
17
+
18
+ should "share a connection if allow_concurrency is set to false" do
19
+ assert !RubyUW::Base.allow_concurrency
20
+ connection = RubyUW::Base.connection
21
+
22
+ a = Thread.new { assert_equal connection, RubyUW::Base.connection }
23
+ b = Thread.new { assert_equal connection, RubyUW::Base.connection }
24
+ a.join
25
+ b.join
26
+ end
27
+
28
+ should "use thread-local connections if allow_concurrency is true" do
29
+ RubyUW::Base.allow_concurrency = true
30
+ assert RubyUW::Base.allow_concurrency
31
+
32
+ connection = RubyUW::Base.connection
33
+ a = Thread.new { assert_not_equal connection, RubyUW::Base.connection }
34
+ b = Thread.new { assert_not_equal connection, RubyUW::Base.connection }
35
+ a.join
36
+ b.join
37
+
38
+ RubyUW::Base.allow_concurrency = false
39
+ end
40
+
41
+ should "return the main thread to store connection on without concurrency" do
42
+ assert !RubyUW::Base.allow_concurrency
43
+ assert_equal Thread.main, RubyUW::Base.connection_thread
44
+
45
+ a = Thread.new { assert_equal Thread.main, RubyUW::Base.connection_thread }
46
+ b = Thread.new { assert_equal Thread.main, RubyUW::Base.connection_thread }
47
+ a.join
48
+ b.join
49
+ end
50
+
51
+ should "return the current thread to store connection on with concurrency" do
52
+ RubyUW::Base.allow_concurrency = true
53
+ assert RubyUW::Base.allow_concurrency
54
+ assert_equal Thread.current, RubyUW::Base.connection_thread
55
+
56
+ a = Thread.new { assert_equal Thread.current, RubyUW::Base.connection_thread }
57
+ b = Thread.new { assert_equal Thread.current, RubyUW::Base.connection_thread }
58
+ a.join
59
+ b.join
60
+
61
+ RubyUW::Base.allow_concurrency = false
62
+ end
63
+
64
+ should "reset connections when changing allow_concurrency value" do
65
+ RubyUW::Base.expects(:reset_connection!).twice
66
+ RubyUW::Base.allow_concurrency = true
67
+ RubyUW::Base.allow_concurrency = false
68
+ end
69
+
70
+ should "not reset connections when not changing allow_concurrency value" do
71
+ RubyUW::Base.expects(:reset_connection!).never
72
+ assert !RubyUW::Base.allow_concurrency #sanity
73
+ RubyUW::Base.allow_concurrency = false
74
+ end
75
+ end
76
+
4
77
  context "with connection" do
5
78
  setup do
6
79
  RubyUW::Base.reset_connection!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubyuw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.99.9
4
+ version: 0.99.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mitchell Hashimoto