Skip to content

Commit 5331632

Browse files
tests: use in-built stats instead of custom helper
Use in-built `crud.stats()` info instead on `storage_stat` helper in tests to track map reduce calls. Part of #224
1 parent f94efcf commit 5331632

File tree

6 files changed

+59
-222
lines changed

6 files changed

+59
-222
lines changed

test/helper.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,14 @@ function helpers.reload_roles(srv)
420420
t.assert_equals({ok, err}, {true, nil})
421421
end
422422

423+
function helpers.get_map_reduces_stat(router, space_name)
424+
return router:eval([[
425+
local stats = require('crud').stats(...)
426+
if stats.select == nil then
427+
return 0
428+
end
429+
return stats.select.details.map_reduces
430+
]], { space_name })
431+
end
432+
423433
return helpers

test/helpers/storage_stat.lua

Lines changed: 0 additions & 118 deletions
This file was deleted.

test/integration/count_test.lua

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ local clock = require('clock')
44
local t = require('luatest')
55

66
local helpers = require('test.helper')
7-
local storage_stat = require('test.helpers.storage_stat')
87

98
local pgroup = t.group('count', {
109
{engine = 'memtx'},
@@ -24,12 +23,9 @@ pgroup.before_all(function(g)
2423

2524
g.cluster:start()
2625

27-
helpers.call_on_storages(g.cluster, function(server)
28-
server.net_box:eval([[
29-
local storage_stat = require('test.helpers.storage_stat')
30-
storage_stat.init_on_storage_for_count()
31-
]])
32-
end)
26+
g.cluster:server('router').net_box:eval([[
27+
require('crud').cfg{ stats = true }
28+
]])
3329
end)
3430

3531
pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
@@ -583,7 +579,8 @@ pgroup.test_count_no_map_reduce = function(g)
583579
},
584580
})
585581

586-
local stat_a = storage_stat.collect(g.cluster)
582+
local router = g.cluster:server('router').net_box
583+
local map_reduces_before = helpers.get_map_reduces_stat(router, 'customers')
587584

588585
-- Case: no conditions, just bucket id.
589586
local result, err = g.cluster.main_server.net_box:call('crud.count', {
@@ -594,15 +591,9 @@ pgroup.test_count_no_map_reduce = function(g)
594591
t.assert_equals(err, nil)
595592
t.assert_equals(result, 1)
596593

597-
local stat_b = storage_stat.collect(g.cluster)
598-
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
599-
['s-1'] = {
600-
requests = 1,
601-
},
602-
['s-2'] = {
603-
requests = 0,
604-
},
605-
})
594+
local map_reduces_after_1 = helpers.get_map_reduces_stat(router, 'customers')
595+
local diff_1 = map_reduces_after_1 - map_reduces_before
596+
t.assert_equals(diff_1, 0, 'Count request was not a map reduce')
606597

607598
-- Case: EQ on secondary index, which is not in the sharding
608599
-- index (primary index in the case).
@@ -614,15 +605,9 @@ pgroup.test_count_no_map_reduce = function(g)
614605
t.assert_equals(err, nil)
615606
t.assert_equals(result, 1)
616607

617-
local stat_c = storage_stat.collect(g.cluster)
618-
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
619-
['s-1'] = {
620-
requests = 0,
621-
},
622-
['s-2'] = {
623-
requests = 1,
624-
},
625-
})
608+
local map_reduces_after_2 = helpers.get_map_reduces_stat(router, 'customers')
609+
local diff_2 = map_reduces_after_2 - map_reduces_after_1
610+
t.assert_equals(diff_2, 0, 'Count request was not a map reduce')
626611
end
627612

628613
pgroup.test_count_timeout = function(g)

test/integration/ddl_sharding_key_test.lua

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ local crud = require('crud')
33
local t = require('luatest')
44

55
local helpers = require('test.helper')
6-
local storage_stat = require('test.helpers.storage_stat')
76

87
local ok = pcall(require, 'ddl')
98
if not ok then
@@ -35,12 +34,9 @@ pgroup.before_all(function(g)
3534
t.assert_equals(type(result), 'table')
3635
t.assert_equals(err, nil)
3736

38-
helpers.call_on_storages(g.cluster, function(server)
39-
server.net_box:eval([[
40-
local storage_stat = require('test.helpers.storage_stat')
41-
storage_stat.init_on_storage_for_select()
42-
]])
43-
end)
37+
g.cluster.main_server.net_box:eval([[
38+
require('crud').cfg{ stats = true }
39+
]])
4440
end)
4541

4642
pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
@@ -367,45 +363,39 @@ for name, case in pairs(cases) do
367363
pgroup[('test_%s_wont_lead_to_map_reduce'):format(name)] = function(g)
368364
case.prepare_data(g, case.space_name)
369365

370-
local stat_a = storage_stat.collect(g.cluster)
366+
local router = g.cluster:server('router').net_box
367+
local map_reduces_before = helpers.get_map_reduces_stat(router, case.space_name)
371368

372-
local result, err = g.cluster.main_server.net_box:call('crud.select', {
369+
local result, err = router:call('crud.select', {
373370
case.space_name, case.conditions
374371
})
375372
t.assert_equals(err, nil)
376373
t.assert_not_equals(result, nil)
377374
t.assert_equals(#result.rows, 1)
378375

379-
local stat_b = storage_stat.collect(g.cluster)
380-
381-
-- Check a number of select() requests made by CRUD on cluster's storages
382-
-- after calling select() on a router. Make sure only a single storage has
383-
-- a single select() request. Otherwise we lead to map-reduce.
384-
local stats = storage_stat.diff(stat_b, stat_a)
385-
t.assert_equals(storage_stat.total(stats), 1, 'Select request was not a map reduce')
376+
local map_reduces_after = helpers.get_map_reduces_stat(router, case.space_name)
377+
local diff = map_reduces_after - map_reduces_before
378+
t.assert_equals(diff, 0, 'Select request was not a map reduce')
386379
end
387380
end
388381

389382
pgroup.test_select_for_part_of_sharding_key_will_lead_to_map_reduce = function(g)
390383
local space_name = 'customers_name_age_key_different_indexes'
391384
prepare_data_name_age_sharding_key(g, space_name)
392385

393-
local stat_a = storage_stat.collect(g.cluster)
386+
local router = g.cluster:server('router').net_box
387+
local map_reduces_before = helpers.get_map_reduces_stat(router, space_name)
394388

395-
local result, err = g.cluster.main_server.net_box:call('crud.select', {
389+
local result, err = router:call('crud.select', {
396390
space_name, {{'==', 'age', 58}},
397391
})
398392
t.assert_equals(err, nil)
399393
t.assert_not_equals(result, nil)
400394
t.assert_equals(#result.rows, 1)
401395

402-
local stat_b = storage_stat.collect(g.cluster)
403-
404-
-- Check a number of select() requests made by CRUD on cluster's storages
405-
-- after calling select() on a router. Make sure it was a map-reduce
406-
-- since we do not have sharding key values in conditions.
407-
local stats = storage_stat.diff(stat_b, stat_a)
408-
t.assert_equals(storage_stat.total(stats), 2, 'Select request was a map reduce')
396+
local map_reduces_after = helpers.get_map_reduces_stat(router, space_name)
397+
local diff = map_reduces_after - map_reduces_before
398+
t.assert_equals(diff, 1, 'Select request was a map reduce')
409399
end
410400

411401
pgroup.test_select_secondary_idx = function(g)

test/integration/pairs_test.lua

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local t = require('luatest')
55
local crud_utils = require('crud.common.utils')
66

77
local helpers = require('test.helper')
8-
local storage_stat = require('test.helpers.storage_stat')
98

109
local pgroup = t.group('pairs', {
1110
{engine = 'memtx'},
@@ -27,12 +26,9 @@ pgroup.before_all(function(g)
2726

2827
g.space_format = g.cluster.servers[2].net_box.space.customers:format()
2928

30-
helpers.call_on_storages(g.cluster, function(server)
31-
server.net_box:eval([[
32-
local storage_stat = require('test.helpers.storage_stat')
33-
storage_stat.init_on_storage_for_select()
34-
]])
35-
end)
29+
g.cluster.main_server.net_box:eval([[
30+
require('crud').cfg{ stats = true }
31+
]])
3632
end)
3733

3834
pgroup.after_all(function(g) helpers.stop_cluster(g.cluster) end)
@@ -842,10 +838,11 @@ pgroup.test_pairs_no_map_reduce = function(g)
842838

843839
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
844840

845-
local stat_a = storage_stat.collect(g.cluster)
841+
local router = g.cluster:server('router').net_box
842+
local map_reduces_before = helpers.get_map_reduces_stat(router, 'customers')
846843

847844
-- Case: no conditions, just bucket id.
848-
local rows = g.cluster.main_server.net_box:eval([[
845+
local rows = router:eval([[
849846
local crud = require('crud')
850847
851848
return crud.pairs(...):totable()
@@ -858,15 +855,9 @@ pgroup.test_pairs_no_map_reduce = function(g)
858855
{3, 2804, 'David', 'Smith', 33, 'Los Angeles'},
859856
})
860857

861-
local stat_b = storage_stat.collect(g.cluster)
862-
t.assert_equals(storage_stat.diff(stat_b, stat_a), {
863-
['s-1'] = {
864-
requests = 1,
865-
},
866-
['s-2'] = {
867-
requests = 0,
868-
},
869-
})
858+
local map_reduces_after_1 = helpers.get_map_reduces_stat(router, 'customers')
859+
local diff_1 = map_reduces_after_1 - map_reduces_before
860+
t.assert_equals(diff_1, 0, 'Select request was not a map reduce')
870861

871862
-- Case: EQ on secondary index, which is not in the sharding
872863
-- index (primary index in the case).
@@ -883,13 +874,7 @@ pgroup.test_pairs_no_map_reduce = function(g)
883874
{4, 1161, 'William', 'White', 81, 'Chicago'},
884875
})
885876

886-
local stat_c = storage_stat.collect(g.cluster)
887-
t.assert_equals(storage_stat.diff(stat_c, stat_b), {
888-
['s-1'] = {
889-
requests = 0,
890-
},
891-
['s-2'] = {
892-
requests = 1,
893-
},
894-
})
877+
local map_reduces_after_2 = helpers.get_map_reduces_stat(router, 'customers')
878+
local diff_2 = map_reduces_after_2 - map_reduces_after_1
879+
t.assert_equals(diff_2, 0, 'Select request was not a map reduce')
895880
end

0 commit comments

Comments
 (0)