prefab-cloud-ruby 0.24.3 → 0.24.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.rubocop.yml +13 -0
  4. data/CHANGELOG.md +78 -0
  5. data/Gemfile.lock +4 -4
  6. data/VERSION +1 -1
  7. data/bin/console +21 -0
  8. data/compile_protos.sh +6 -0
  9. data/lib/prefab/client.rb +25 -4
  10. data/lib/prefab/config_client.rb +17 -6
  11. data/lib/prefab/config_loader.rb +1 -1
  12. data/lib/prefab/config_resolver.rb +2 -4
  13. data/lib/prefab/config_value_wrapper.rb +18 -0
  14. data/lib/prefab/context.rb +22 -2
  15. data/lib/prefab/context_shape.rb +20 -0
  16. data/lib/prefab/context_shape_aggregator.rb +63 -0
  17. data/lib/prefab/criteria_evaluator.rb +61 -41
  18. data/lib/prefab/evaluated_configs_aggregator.rb +60 -0
  19. data/lib/prefab/evaluated_keys_aggregator.rb +41 -0
  20. data/lib/prefab/http_connection.rb +5 -1
  21. data/lib/prefab/local_config_parser.rb +13 -13
  22. data/lib/prefab/log_path_aggregator.rb +64 -0
  23. data/lib/prefab/logger_client.rb +11 -13
  24. data/lib/prefab/options.rb +37 -1
  25. data/lib/prefab/periodic_sync.rb +51 -0
  26. data/lib/prefab/time_helpers.rb +7 -0
  27. data/lib/prefab-cloud-ruby.rb +9 -2
  28. data/lib/prefab_pb.rb +33 -220
  29. data/prefab-cloud-ruby.gemspec +21 -5
  30. data/test/test_config_loader.rb +15 -15
  31. data/test/test_config_resolver.rb +102 -102
  32. data/test/test_config_value_unwrapper.rb +13 -13
  33. data/test/test_context.rb +42 -0
  34. data/test/test_context_shape.rb +51 -0
  35. data/test/test_context_shape_aggregator.rb +137 -0
  36. data/test/test_criteria_evaluator.rb +253 -150
  37. data/test/test_evaluated_configs_aggregator.rb +254 -0
  38. data/test/test_evaluated_keys_aggregator.rb +54 -0
  39. data/test/test_helper.rb +34 -2
  40. data/test/test_log_path_aggregator.rb +57 -0
  41. data/test/test_logger.rb +33 -33
  42. data/test/test_weighted_value_resolver.rb +2 -2
  43. metadata +21 -5
  44. data/lib/prefab/log_path_collector.rb +0 -102
  45. data/test/test_log_path_collector.rb +0 -58
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ class TestContextShapeAggregator < Minitest::Test
6
+ DOB = Date.new
7
+
8
+ CONTEXT_1 = Prefab::Context.new({
9
+ 'user' => {
10
+ 'name' => 'user-name',
11
+ 'email' => 'user.email',
12
+ 'age' => 42.5
13
+ },
14
+ 'subscription' => {
15
+ 'plan' => 'advanced',
16
+ 'free' => false
17
+ }
18
+ }).freeze
19
+
20
+ CONTEXT_2 = Prefab::Context.new({
21
+ 'user' => {
22
+ 'name' => 'other-user-name',
23
+ 'dob' => DOB
24
+ },
25
+ 'device' => {
26
+ 'name' => 'device-name',
27
+ 'os' => 'os-name',
28
+ 'version' => 3
29
+ }
30
+ }).freeze
31
+
32
+ CONTEXT_3 = Prefab::Context.new({
33
+ 'subscription' => {
34
+ 'plan' => 'pro',
35
+ 'trial' => true
36
+ }
37
+ }).freeze
38
+
39
+ def test_push
40
+ aggregator = new_aggregator(max_shapes: 9)
41
+
42
+ aggregator.push(CONTEXT_1)
43
+ aggregator.push(CONTEXT_2)
44
+ assert_equal 9, aggregator.data.size
45
+
46
+ # we've reached the limit so no more
47
+ aggregator.push(CONTEXT_3)
48
+ assert_equal 9, aggregator.data.size
49
+
50
+ assert_equal [['user', 'name', 2], ['user', 'email', 2], ['user', 'age', 4], ['subscription', 'plan', 2], ['subscription', 'free', 5], ['user', 'dob', 2], ['device', 'name', 2], ['device', 'os', 2], ['device', 'version', 1]],
51
+ aggregator.data.to_a
52
+ end
53
+
54
+ def test_prepare_data
55
+ aggregator = new_aggregator
56
+
57
+ aggregator.push(CONTEXT_1)
58
+ aggregator.push(CONTEXT_2)
59
+ aggregator.push(CONTEXT_3)
60
+
61
+ data = aggregator.prepare_data
62
+
63
+ assert_equal %w[user subscription device], data.keys
64
+
65
+ assert_equal data['user'], {
66
+ 'name' => 2,
67
+ 'email' => 2,
68
+ 'dob' => 2,
69
+ 'age' => 4
70
+ }
71
+
72
+ assert_equal data['subscription'], {
73
+ 'plan' => 2,
74
+ 'trial' => 5,
75
+ 'free' => 5
76
+ }
77
+
78
+ assert_equal data['device'], {
79
+ 'name' => 2,
80
+ 'os' => 2,
81
+ 'version' => 1
82
+ }
83
+
84
+ assert_equal [], aggregator.data.to_a
85
+ end
86
+
87
+ def test_sync
88
+ client = new_client
89
+
90
+ client.get 'some.key', 'default', CONTEXT_1
91
+ client.get 'some.key', 'default', CONTEXT_2
92
+ client.get 'some.key', 'default', CONTEXT_3
93
+
94
+ requests = wait_for_post_requests(client) do
95
+ client.context_shape_aggregator.send(:sync)
96
+ end
97
+
98
+ assert_equal [
99
+ [
100
+ '/api/v1/context-shapes',
101
+ PrefabProto::ContextShapes.new(shapes: [
102
+ PrefabProto::ContextShape.new(
103
+ name: 'user', field_types: {
104
+ 'age' => 4, 'dob' => 2, 'email' => 2, 'name' => 2
105
+ }
106
+ ),
107
+ PrefabProto::ContextShape.new(
108
+ name: 'subscription', field_types: {
109
+ 'plan' => 2, 'free' => 5, 'trial' => 5
110
+ }
111
+ ),
112
+ PrefabProto::ContextShape.new(
113
+ name: 'device', field_types: {
114
+ 'version' => 1, 'os' => 2, 'name' => 2
115
+ }
116
+ )
117
+ ])
118
+ ]
119
+ ], requests
120
+ end
121
+
122
+ private
123
+
124
+ def new_client(overrides = {})
125
+ super(**{
126
+ prefab_datasources: Prefab::Options::DATASOURCES::ALL,
127
+ initialization_timeout_sec: 0,
128
+ on_init_failure: Prefab::Options::ON_INITIALIZATION_FAILURE::RETURN,
129
+ api_key: '123-development-yourapikey-SDK',
130
+ shape_sync_interval: 1000 # we'll trigger sync manually in our test
131
+ }.merge(overrides))
132
+ end
133
+
134
+ def new_aggregator(max_shapes: 1000)
135
+ Prefab::ContextShapeAggregator.new(client: new_client, sync_interval: 1000, max_shapes: max_shapes)
136
+ end
137
+ end