azure_application_insights 0.5.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/gem-push.yml +34 -0
  3. data/.gitignore +40 -0
  4. data/.travis.yml +22 -0
  5. data/CHANGELOG.md +17 -0
  6. data/CONTRIBUTING.md +68 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +11 -0
  9. data/README.md +161 -0
  10. data/Rakefile +15 -0
  11. data/application_insights.gemspec +30 -0
  12. data/lib/application_insights/channel/asynchronous_queue.rb +58 -0
  13. data/lib/application_insights/channel/asynchronous_sender.rb +133 -0
  14. data/lib/application_insights/channel/contracts/application.rb +14 -0
  15. data/lib/application_insights/channel/contracts/cloud.rb +14 -0
  16. data/lib/application_insights/channel/contracts/data.rb +14 -0
  17. data/lib/application_insights/channel/contracts/data_point.rb +24 -0
  18. data/lib/application_insights/channel/contracts/data_point_type.rb +7 -0
  19. data/lib/application_insights/channel/contracts/dependency_kind.rb +9 -0
  20. data/lib/application_insights/channel/contracts/dependency_source_type.rb +9 -0
  21. data/lib/application_insights/channel/contracts/device.rb +28 -0
  22. data/lib/application_insights/channel/contracts/envelope.rb +40 -0
  23. data/lib/application_insights/channel/contracts/event_data.rb +28 -0
  24. data/lib/application_insights/channel/contracts/exception_data.rb +37 -0
  25. data/lib/application_insights/channel/contracts/exception_details.rb +28 -0
  26. data/lib/application_insights/channel/contracts/internal.rb +14 -0
  27. data/lib/application_insights/channel/contracts/json_serializable.rb +59 -0
  28. data/lib/application_insights/channel/contracts/location.rb +16 -0
  29. data/lib/application_insights/channel/contracts/message_data.rb +24 -0
  30. data/lib/application_insights/channel/contracts/metric_data.rb +27 -0
  31. data/lib/application_insights/channel/contracts/operation.rb +19 -0
  32. data/lib/application_insights/channel/contracts/page_view_data.rb +30 -0
  33. data/lib/application_insights/channel/contracts/remote_dependency_data.rb +56 -0
  34. data/lib/application_insights/channel/contracts/request_data.rb +36 -0
  35. data/lib/application_insights/channel/contracts/session.rb +15 -0
  36. data/lib/application_insights/channel/contracts/severity_level.rb +13 -0
  37. data/lib/application_insights/channel/contracts/stack_frame.rb +17 -0
  38. data/lib/application_insights/channel/contracts/user.rb +19 -0
  39. data/lib/application_insights/channel/event.rb +68 -0
  40. data/lib/application_insights/channel/queue_base.rb +73 -0
  41. data/lib/application_insights/channel/sender_base.rb +88 -0
  42. data/lib/application_insights/channel/synchronous_queue.rb +45 -0
  43. data/lib/application_insights/channel/synchronous_sender.rb +17 -0
  44. data/lib/application_insights/channel/telemetry_channel.rb +131 -0
  45. data/lib/application_insights/channel/telemetry_context.rb +85 -0
  46. data/lib/application_insights/rack/track_request.rb +158 -0
  47. data/lib/application_insights/telemetry_client.rb +229 -0
  48. data/lib/application_insights/unhandled_exception.rb +49 -0
  49. data/lib/application_insights/version.rb +3 -0
  50. data/lib/application_insights.rb +9 -0
  51. data/test/application_insights/channel/contracts/test_application.rb +44 -0
  52. data/test/application_insights/channel/contracts/test_cloud.rb +44 -0
  53. data/test/application_insights/channel/contracts/test_data.rb +44 -0
  54. data/test/application_insights/channel/contracts/test_data_point.rb +109 -0
  55. data/test/application_insights/channel/contracts/test_device.rb +200 -0
  56. data/test/application_insights/channel/contracts/test_envelope.rb +209 -0
  57. data/test/application_insights/channel/contracts/test_event_data.rb +62 -0
  58. data/test/application_insights/channel/contracts/test_exception_data.rb +111 -0
  59. data/test/application_insights/channel/contracts/test_exception_details.rb +106 -0
  60. data/test/application_insights/channel/contracts/test_internal.rb +44 -0
  61. data/test/application_insights/channel/contracts/test_location.rb +70 -0
  62. data/test/application_insights/channel/contracts/test_message_data.rb +66 -0
  63. data/test/application_insights/channel/contracts/test_metric_data.rb +50 -0
  64. data/test/application_insights/channel/contracts/test_operation.rb +109 -0
  65. data/test/application_insights/channel/contracts/test_page_view_data.rb +88 -0
  66. data/test/application_insights/channel/contracts/test_remote_dependency_data.rb +209 -0
  67. data/test/application_insights/channel/contracts/test_request_data.rb +153 -0
  68. data/test/application_insights/channel/contracts/test_session.rb +57 -0
  69. data/test/application_insights/channel/contracts/test_stack_frame.rb +83 -0
  70. data/test/application_insights/channel/contracts/test_user.rb +96 -0
  71. data/test/application_insights/channel/test_asynchronous_queue.rb +47 -0
  72. data/test/application_insights/channel/test_asynchronous_sender.rb +81 -0
  73. data/test/application_insights/channel/test_event.rb +53 -0
  74. data/test/application_insights/channel/test_queue_base.rb +89 -0
  75. data/test/application_insights/channel/test_sender_base.rb +94 -0
  76. data/test/application_insights/channel/test_synchronous_queue.rb +28 -0
  77. data/test/application_insights/channel/test_synchronous_sender.rb +11 -0
  78. data/test/application_insights/channel/test_telemetry_channel.rb +167 -0
  79. data/test/application_insights/channel/test_telemetry_context.rb +83 -0
  80. data/test/application_insights/mock_sender.rb +37 -0
  81. data/test/application_insights/rack/test_track_request.rb +182 -0
  82. data/test/application_insights/test_logger.rb +10 -0
  83. data/test/application_insights/test_telemetry_client.rb +138 -0
  84. data/test/application_insights/test_unhandled_exception.rb +23 -0
  85. data/test/application_insights.rb +8 -0
  86. metadata +247 -0
@@ -0,0 +1,153 @@
1
+ require_relative '../../../../lib/application_insights/channel/contracts/request_data'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestRequestData < Test::Unit::TestCase
7
+ def test_initialize
8
+ item = Contracts::RequestData.new
9
+ assert_not_nil item
10
+ end
11
+
12
+ def test_ver_works_as_expected
13
+ expected = 42
14
+ item = Contracts::RequestData.new
15
+ item.ver = expected
16
+ actual = item.ver
17
+ assert_equal expected, actual
18
+ expected = 13
19
+ item.ver = expected
20
+ actual = item.ver
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_id_works_as_expected
25
+ expected = 'Test string'
26
+ item = Contracts::RequestData.new
27
+ item.id = expected
28
+ actual = item.id
29
+ assert_equal expected, actual
30
+ expected = 'Other string'
31
+ item.id = expected
32
+ actual = item.id
33
+ assert_equal expected, actual
34
+ end
35
+
36
+ def test_name_works_as_expected
37
+ expected = 'Test string'
38
+ item = Contracts::RequestData.new
39
+ item.name = expected
40
+ actual = item.name
41
+ assert_equal expected, actual
42
+ expected = 'Other string'
43
+ item.name = expected
44
+ actual = item.name
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ def test_start_time_works_as_expected
49
+ expected = 'Test string'
50
+ item = Contracts::RequestData.new
51
+ item.start_time = expected
52
+ actual = item.start_time
53
+ assert_equal expected, actual
54
+ expected = 'Other string'
55
+ item.start_time = expected
56
+ actual = item.start_time
57
+ assert_equal expected, actual
58
+ end
59
+
60
+ def test_duration_works_as_expected
61
+ expected = 'Test string'
62
+ item = Contracts::RequestData.new
63
+ item.duration = expected
64
+ actual = item.duration
65
+ assert_equal expected, actual
66
+ expected = 'Other string'
67
+ item.duration = expected
68
+ actual = item.duration
69
+ assert_equal expected, actual
70
+ end
71
+
72
+ def test_response_code_works_as_expected
73
+ expected = 'Test string'
74
+ item = Contracts::RequestData.new
75
+ item.response_code = expected
76
+ actual = item.response_code
77
+ assert_equal expected, actual
78
+ expected = 'Other string'
79
+ item.response_code = expected
80
+ actual = item.response_code
81
+ assert_equal expected, actual
82
+ end
83
+
84
+ def test_success_works_as_expected
85
+ expected = true
86
+ item = Contracts::RequestData.new
87
+ item.success = expected
88
+ actual = item.success
89
+ assert_equal expected, actual
90
+ expected = false
91
+ item.success = expected
92
+ actual = item.success
93
+ assert_equal expected, actual
94
+ end
95
+
96
+ def test_http_method_works_as_expected
97
+ expected = 'Test string'
98
+ item = Contracts::RequestData.new
99
+ item.http_method = expected
100
+ actual = item.http_method
101
+ assert_equal expected, actual
102
+ expected = 'Other string'
103
+ item.http_method = expected
104
+ actual = item.http_method
105
+ assert_equal expected, actual
106
+ end
107
+
108
+ def test_url_works_as_expected
109
+ expected = 'Test string'
110
+ item = Contracts::RequestData.new
111
+ item.url = expected
112
+ actual = item.url
113
+ assert_equal expected, actual
114
+ expected = 'Other string'
115
+ item.url = expected
116
+ actual = item.url
117
+ assert_equal expected, actual
118
+ end
119
+
120
+ def test_properties_works_as_expected
121
+ item = Contracts::RequestData.new
122
+ actual = item.properties
123
+ assert_not_nil actual
124
+ end
125
+
126
+ def test_measurements_works_as_expected
127
+ item = Contracts::RequestData.new
128
+ actual = item.measurements
129
+ assert_not_nil actual
130
+ end
131
+
132
+ def test_to_json_works_as_expected
133
+ item = Contracts::RequestData.new
134
+ item.ver = 42
135
+ item.id = 'Test string'
136
+ item.name = 'Test string'
137
+ item.start_time = 'Test string'
138
+ item.duration = 'Test string'
139
+ item.response_code = 'Test string'
140
+ item.success = true
141
+ item.http_method = 'Test string'
142
+ item.url = 'Test string'
143
+ { 'key1' => 'test value 1' , 'key2' => 'test value 2' }.each do |key, value|
144
+ item.properties[key] = value
145
+ end
146
+ { 'key1' => 3.1415 , 'key2' => 42.2 }.each do |key, value|
147
+ item.measurements[key] = value
148
+ end
149
+ actual = item.to_json
150
+ expected = '{"ver":42,"id":"Test string","name":"Test string","startTime":"Test string","duration":"Test string","responseCode":"Test string","success":true,"httpMethod":"Test string","url":"Test string","properties":{"key1":"test value 1","key2":"test value 2"},"measurements":{"key1":3.1415,"key2":42.2}}'
151
+ assert_equal expected, actual
152
+ end
153
+ end
@@ -0,0 +1,57 @@
1
+ require_relative '../../../../lib/application_insights/channel/contracts/session'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestSession < Test::Unit::TestCase
7
+ def test_initialize
8
+ item = Contracts::Session.new
9
+ assert_not_nil item
10
+ end
11
+
12
+ def test_id_works_as_expected
13
+ expected = 'Test string'
14
+ item = Contracts::Session.new
15
+ item.id = expected
16
+ actual = item.id
17
+ assert_equal expected, actual
18
+ expected = 'Other string'
19
+ item.id = expected
20
+ actual = item.id
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_is_first_works_as_expected
25
+ expected = 'Test string'
26
+ item = Contracts::Session.new
27
+ item.is_first = expected
28
+ actual = item.is_first
29
+ assert_equal expected, actual
30
+ expected = 'Other string'
31
+ item.is_first = expected
32
+ actual = item.is_first
33
+ assert_equal expected, actual
34
+ end
35
+
36
+ def test_is_new_works_as_expected
37
+ expected = 'Test string'
38
+ item = Contracts::Session.new
39
+ item.is_new = expected
40
+ actual = item.is_new
41
+ assert_equal expected, actual
42
+ expected = 'Other string'
43
+ item.is_new = expected
44
+ actual = item.is_new
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ def test_to_json_works_as_expected
49
+ item = Contracts::Session.new
50
+ item.id = 'Test string'
51
+ item.is_first = 'Test string'
52
+ item.is_new = 'Test string'
53
+ actual = item.to_json
54
+ expected = '{"ai.session.id":"Test string","ai.session.isFirst":"Test string","ai.session.isNew":"Test string"}'
55
+ assert_equal expected, actual
56
+ end
57
+ end
@@ -0,0 +1,83 @@
1
+ require_relative '../../../../lib/application_insights/channel/contracts/stack_frame'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestStackFrame < Test::Unit::TestCase
7
+ def test_initialize
8
+ item = Contracts::StackFrame.new
9
+ assert_not_nil item
10
+ end
11
+
12
+ def test_level_works_as_expected
13
+ expected = 42
14
+ item = Contracts::StackFrame.new
15
+ item.level = expected
16
+ actual = item.level
17
+ assert_equal expected, actual
18
+ expected = 13
19
+ item.level = expected
20
+ actual = item.level
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_method_works_as_expected
25
+ expected = 'Test string'
26
+ item = Contracts::StackFrame.new
27
+ item.method = expected
28
+ actual = item.method
29
+ assert_equal expected, actual
30
+ expected = 'Other string'
31
+ item.method = expected
32
+ actual = item.method
33
+ assert_equal expected, actual
34
+ end
35
+
36
+ def test_assembly_works_as_expected
37
+ expected = 'Test string'
38
+ item = Contracts::StackFrame.new
39
+ item.assembly = expected
40
+ actual = item.assembly
41
+ assert_equal expected, actual
42
+ expected = 'Other string'
43
+ item.assembly = expected
44
+ actual = item.assembly
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ def test_file_name_works_as_expected
49
+ expected = 'Test string'
50
+ item = Contracts::StackFrame.new
51
+ item.file_name = expected
52
+ actual = item.file_name
53
+ assert_equal expected, actual
54
+ expected = 'Other string'
55
+ item.file_name = expected
56
+ actual = item.file_name
57
+ assert_equal expected, actual
58
+ end
59
+
60
+ def test_line_works_as_expected
61
+ expected = 42
62
+ item = Contracts::StackFrame.new
63
+ item.line = expected
64
+ actual = item.line
65
+ assert_equal expected, actual
66
+ expected = 13
67
+ item.line = expected
68
+ actual = item.line
69
+ assert_equal expected, actual
70
+ end
71
+
72
+ def test_to_json_works_as_expected
73
+ item = Contracts::StackFrame.new
74
+ item.level = 42
75
+ item.method = 'Test string'
76
+ item.assembly = 'Test string'
77
+ item.file_name = 'Test string'
78
+ item.line = 42
79
+ actual = item.to_json
80
+ expected = '{"level":42,"method":"Test string","assembly":"Test string","fileName":"Test string","line":42}'
81
+ assert_equal expected, actual
82
+ end
83
+ end
@@ -0,0 +1,96 @@
1
+ require_relative '../../../../lib/application_insights/channel/contracts/user'
2
+ require 'test/unit'
3
+
4
+ include ApplicationInsights::Channel
5
+
6
+ class TestUser < Test::Unit::TestCase
7
+ def test_initialize
8
+ item = Contracts::User.new
9
+ assert_not_nil item
10
+ end
11
+
12
+ def test_account_acquisition_date_works_as_expected
13
+ expected = 'Test string'
14
+ item = Contracts::User.new
15
+ item.account_acquisition_date = expected
16
+ actual = item.account_acquisition_date
17
+ assert_equal expected, actual
18
+ expected = 'Other string'
19
+ item.account_acquisition_date = expected
20
+ actual = item.account_acquisition_date
21
+ assert_equal expected, actual
22
+ end
23
+
24
+ def test_account_id_works_as_expected
25
+ expected = 'Test string'
26
+ item = Contracts::User.new
27
+ item.account_id = expected
28
+ actual = item.account_id
29
+ assert_equal expected, actual
30
+ expected = 'Other string'
31
+ item.account_id = expected
32
+ actual = item.account_id
33
+ assert_equal expected, actual
34
+ end
35
+
36
+ def test_user_agent_works_as_expected
37
+ expected = 'Test string'
38
+ item = Contracts::User.new
39
+ item.user_agent = expected
40
+ actual = item.user_agent
41
+ assert_equal expected, actual
42
+ expected = 'Other string'
43
+ item.user_agent = expected
44
+ actual = item.user_agent
45
+ assert_equal expected, actual
46
+ end
47
+
48
+ def test_id_works_as_expected
49
+ expected = 'Test string'
50
+ item = Contracts::User.new
51
+ item.id = expected
52
+ actual = item.id
53
+ assert_equal expected, actual
54
+ expected = 'Other string'
55
+ item.id = expected
56
+ actual = item.id
57
+ assert_equal expected, actual
58
+ end
59
+
60
+ def test_user_auth_id_as_expected
61
+ expected = 'Test string'
62
+ item = Contracts::User.new
63
+ item.auth_user_id = expected
64
+ actual = item.auth_user_id
65
+ assert_equal expected, actual
66
+ expected = 'Other string'
67
+ item.auth_user_id = expected
68
+ actual = item.auth_user_id
69
+ assert_equal expected, actual
70
+ end
71
+
72
+ def test_store_region_works_as_expected
73
+ expected = 'Test string'
74
+ item = Contracts::User.new
75
+ item.store_region = expected
76
+ actual = item.store_region
77
+ assert_equal expected, actual
78
+ expected = 'Other string'
79
+ item.store_region = expected
80
+ actual = item.store_region
81
+ assert_equal expected, actual
82
+ end
83
+
84
+ def test_to_json_works_as_expected
85
+ item = Contracts::User.new
86
+ item.account_acquisition_date = 'Test string'
87
+ item.account_id = 'Test string'
88
+ item.user_agent = 'Test string'
89
+ item.id = 'Test string'
90
+ item.store_region = 'Test string'
91
+ item.auth_user_id = 'Test string'
92
+ actual = item.to_json
93
+ expected = '{"ai.user.accountAcquisitionDate":"Test string","ai.user.accountId":"Test string","ai.user.userAgent":"Test string","ai.user.id":"Test string","ai.user.authUserId":"Test string","ai.user.storeRegion":"Test string"}'
94
+ assert_equal expected, actual
95
+ end
96
+ end
@@ -0,0 +1,47 @@
1
+ require_relative '../../../lib/application_insights/channel/sender_base'
2
+ require_relative '../../../lib/application_insights/channel/asynchronous_queue'
3
+ require_relative '../mock_sender'
4
+ require 'test/unit'
5
+
6
+ include ApplicationInsights::Channel
7
+
8
+ class TestAsynchronousQueue < Test::Unit::TestCase
9
+ def test_initialize
10
+ queue = AsynchronousQueue.new MockAsynchronousSender.new
11
+ assert_not_nil queue.flush_notification
12
+ end
13
+
14
+ def test_flush_notification_works_as_expected
15
+ queue = AsynchronousQueue.new MockAsynchronousSender.new
16
+ assert_not_nil queue.flush_notification
17
+ result = queue.flush_notification.wait 1
18
+ assert_equal false, result
19
+ queue.flush_notification.set
20
+ result = queue.flush_notification.wait
21
+ assert_equal true, result
22
+ queue.flush_notification.clear
23
+ result = queue.flush_notification.wait 1
24
+ assert_equal false, result
25
+ end
26
+
27
+ def test_push_works_As_expected
28
+ sender = MockAsynchronousSender.new
29
+ queue = AsynchronousQueue.new sender
30
+ queue.push 42
31
+ assert_equal 1, sender.start_call_count
32
+ assert_equal 42, queue.pop
33
+ assert_nil queue.pop
34
+ end
35
+
36
+ def test_flush_works_as_expected
37
+ sender = MockAsynchronousSender.new
38
+ queue = AsynchronousQueue.new sender
39
+ assert_not_nil queue.flush_notification
40
+ result = queue.flush_notification.wait 1
41
+ assert_equal false, result
42
+ queue.flush
43
+ assert_equal 1, sender.start_call_count
44
+ result = queue.flush_notification.wait
45
+ assert_equal true, result
46
+ end
47
+ end
@@ -0,0 +1,81 @@
1
+ require_relative '../../../lib/application_insights/channel/sender_base'
2
+ require_relative '../../../lib/application_insights/channel/asynchronous_sender'
3
+ require_relative '../../../lib/application_insights/channel/asynchronous_queue'
4
+ require 'test/unit'
5
+
6
+ include ApplicationInsights::Channel
7
+
8
+ class TestAsynchronousSender < Test::Unit::TestCase
9
+ def test_initialize
10
+ sender = AsynchronousSender.new
11
+ assert_equal 'https://dc.services.visualstudio.com/v2/track', sender.service_endpoint_uri
12
+ assert_equal 1.0, sender.send_interval
13
+ assert_equal 3.0, sender.send_time
14
+ end
15
+
16
+ def test_send_interval_works_as_expected
17
+ sender = AsynchronousSender.new
18
+ assert_equal 1.0, sender.send_interval
19
+ sender.send_interval = 10.0
20
+ assert_equal 10.0, sender.send_interval
21
+ end
22
+
23
+ def test_send_time_works_as_expected
24
+ sender = AsynchronousSender.new
25
+ assert_equal 3.0, sender.send_time
26
+ sender.send_time = 10.0
27
+ assert_equal 10.0, sender.send_time
28
+ end
29
+
30
+ def test_work_thread_works_as_expected
31
+ sender = InterceptableAsynchronousSender.new
32
+ assert_nil sender.work_thread
33
+ sender.send_interval = 1.0
34
+ sender.send_time = 3.0
35
+ queue = AsynchronousQueue.new sender
36
+ sender.invoke_base_start = true
37
+ queue.push 1
38
+ assert_not_nil sender.work_thread
39
+ sleep 5.0
40
+ assert_nil sender.work_thread
41
+ end
42
+
43
+ def test_start
44
+ sender = InterceptableAsynchronousSender.new
45
+ sender.send_interval = 1.0
46
+ sender.send_time = 3.0
47
+ queue = AsynchronousQueue.new sender
48
+ sender.invoke_base_start = false
49
+ queue.push 1
50
+ queue.push 2
51
+ sender.invoke_base_start = true
52
+ sender.start
53
+ sleep 2.0
54
+ queue.push 3
55
+ sleep 5.0
56
+ data = sender.data_to_send
57
+ assert_equal [[1, 2], [3]], data
58
+ end
59
+
60
+ end
61
+
62
+ class InterceptableAsynchronousSender < AsynchronousSender
63
+ def initialize
64
+ @data_to_send = []
65
+ @invoke_base_start = false
66
+ super
67
+ end
68
+
69
+ attr_accessor :invoke_base_start
70
+ attr_accessor :data_to_send
71
+
72
+ def start
73
+ if invoke_base_start
74
+ super
75
+ end
76
+ end
77
+
78
+ def send(data_to_send)
79
+ @data_to_send.push data_to_send
80
+ end
81
+ end
@@ -0,0 +1,53 @@
1
+ require_relative '../../../lib/application_insights/channel/sender_base'
2
+ require_relative '../../../lib/application_insights/channel/asynchronous_queue'
3
+ require 'thread'
4
+ require 'test/unit'
5
+
6
+ include ApplicationInsights::Channel
7
+
8
+ class TestEvent < Test::Unit::TestCase
9
+ def test_initialize
10
+ event = Event.new
11
+ assert_equal false, event.signal
12
+ end
13
+
14
+ def test_set_works_as_expected
15
+ event = Event.new
16
+ assert_equal false, event.signal
17
+ Thread.new do
18
+ sleep 1
19
+ event.set
20
+ end
21
+ result = event.wait 2.0
22
+ assert_equal true, result
23
+ end
24
+
25
+ def test_clear_works_as_expected
26
+ event = Event.new
27
+ assert_equal false, event.signal
28
+ event.set
29
+ assert_equal true, event.signal
30
+ event.clear
31
+ assert_equal false, event.signal
32
+ end
33
+
34
+ def test_wait_works_as_expected
35
+ event1 = Event.new
36
+ assert_equal false, event1.signal
37
+ Thread.new do
38
+ sleep 1
39
+ event1.set
40
+ end
41
+ result = event1.wait 0.5
42
+ assert_equal false, result
43
+
44
+ event2 = Event.new
45
+ assert_equal false, event1.signal
46
+ Thread.new do
47
+ sleep 1
48
+ event2.set
49
+ end
50
+ result = event2.wait 2.0
51
+ assert_equal true, result
52
+ end
53
+ end
@@ -0,0 +1,89 @@
1
+ require_relative '../../../lib/application_insights/channel/queue_base'
2
+ require_relative '../../../lib/application_insights/channel/sender_base'
3
+ require 'test/unit'
4
+
5
+ include ApplicationInsights::Channel
6
+
7
+ class TestQueueBase < Test::Unit::TestCase
8
+ def test_initialize
9
+ sender = SenderBase.new 'http://tempuri.org'
10
+ item = QueueBase.new sender
11
+ assert_equal sender.queue, item
12
+ assert_equal 500, item.max_queue_length
13
+ assert_equal sender, item.sender
14
+ end
15
+
16
+ def test_max_queue_length_works_as_expected
17
+ sender = SenderBase.new 'http://tempuri.org'
18
+ item = QueueBase.new sender
19
+ assert_equal 500, item.max_queue_length
20
+ item.max_queue_length = 42
21
+ assert_equal 42, item.max_queue_length
22
+ end
23
+
24
+ def test_sender_works_as_expected
25
+ sender = SenderBase.new 'http://tempuri.org'
26
+ item = QueueBase.new sender
27
+ assert_equal sender, item.sender
28
+ end
29
+
30
+ def test_push_will_not_enqueue_nil
31
+ sender = SenderBase.new 'http://tempuri.org'
32
+ item = QueueBase.new sender
33
+ queue = item.instance_variable_get('@queue')
34
+ item.push nil
35
+ assert queue.empty?
36
+ end
37
+
38
+ def test_push_works_as_expected
39
+ sender = SenderBase.new 'http://tempuri.org'
40
+ item = MockQueueBase.new sender
41
+ item.max_queue_length = 2
42
+ queue = item.instance_variable_get('@queue')
43
+ [[7, 0], [8, 1], [9, 2]].each do |i, count|
44
+ item.push i
45
+ assert_equal count, item.flush_count
46
+ end
47
+ assert_equal 3, queue.length
48
+ temp = []
49
+ queue.length.times do |i|
50
+ temp.push queue.pop
51
+ end
52
+ assert_equal [7, 8, 9], temp
53
+ end
54
+
55
+ def test_pop_works_as_expected
56
+ sender = SenderBase.new 'http://tempuri.org'
57
+ item = QueueBase.new sender
58
+ queue = item.instance_variable_get('@queue')
59
+ [7, 8, 9].each do |i|
60
+ queue.push i
61
+ end
62
+ [7, 8, 9, nil].each do |i|
63
+ assert_equal i, item.pop
64
+ end
65
+ end
66
+
67
+ def test_empty_works_as_expected
68
+ sender = SenderBase.new 'http://tempuri.org'
69
+ queue = QueueBase.new sender
70
+ assert_equal true, queue.empty?
71
+ queue.push 1
72
+ assert_equal false, queue.empty?
73
+ queue.pop
74
+ assert_equal true, queue.empty?
75
+ end
76
+ end
77
+
78
+ class MockQueueBase < QueueBase
79
+ def initialize(sender)
80
+ super sender
81
+ @flush_count = 0
82
+ end
83
+
84
+ attr_accessor :flush_count
85
+
86
+ def flush
87
+ @flush_count += 1
88
+ end
89
+ end