File: test_NextHopConsistentHash.cc

package info (click to toggle)
trafficserver 9.2.5%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 53,008 kB
  • sloc: cpp: 345,484; ansic: 31,134; python: 24,200; sh: 7,271; makefile: 3,045; perl: 2,261; java: 277; pascal: 119; sql: 94; xml: 2
file content (672 lines) | stat: -rw-r--r-- 25,298 bytes parent folder | download | duplicates (2)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/** @file

  Unit tests for the NextHopConsistentHash.

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  @section details Details

  Unit testing the NextHopConsistentHash class.

 */

#define CATCH_CONFIG_MAIN /* include main function */

#include <catch.hpp> /* catch unit-test framework */
#include <yaml-cpp/yaml.h>

#include "HttpSM.h"
#include "nexthop_test_stubs.h"
#include "NextHopSelectionStrategy.h"
#include "NextHopStrategyFactory.h"
#include "NextHopConsistentHash.h"

#include "HTTP.h"
extern int cmd_disable_pfreelist;

SCENARIO("Testing NextHopConsistentHash class, using policy 'consistent_hash'", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    // load the configuration strtegies.
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("consistent-hash-1");

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 3);
      }
    }

    WHEN("requests are received.")
    {
      // need to run these checks in succession so there
      // are no host status state changes.
      //
      // These tests simulate failed requests using a selected host.
      // markNextHop() is called by the state machine when
      // there is a request failure due to a connection error or
      // timeout.  the 'result' struct has the information on the
      // host used in the failed request and when called, marks the
      // host indicated in the 'request' struct as unavailable.
      //
      // Here we walk through making requests then marking the selected
      // host down until all are down and the origin is finally chosen.
      //
      THEN("when making requests and taking nodes down.")
      {
        HttpSM sm;
        ParentResult *result = &sm.t_state.parent_result;
        TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);

        // first request.
        build_request(10001, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "p1.foo.com") == 0);

        // mark down p1.foo.com.  markNextHop looks at the 'result'
        // and uses the host index there mark down the host selected
        // from a
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // second request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10002, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "p2.foo.com") == 0);

        // mark down p2.foo.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // third request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10003, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "s2.bar.com") == 0);

        // mark down s2.bar.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // fourth request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10004, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "s1.bar.com") == 0);

        // mark down s1.bar.com.
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // fifth request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10005, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "q1.bar.com") == 0);

        // mark down q1.bar.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // sixth request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10006, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "q2.bar.com") == 0);

        // mark down q2.bar.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // seventh request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10007, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        CHECK(result->result == ParentResultType::PARENT_DIRECT);
        CHECK(result->hostname == nullptr);

        // sleep and test that q2 is becomes retryable;
        time_t now = time(nullptr) + 5;

        // eighth request - reusing the ParentResult from the last request
        // simulating a failure triggers a search for another parent, not firstcall.
        build_request(10008, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp, nullptr, now);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "q2.bar.com") == 0);

        // free up request resources.
        br_destroy(sm);
      }
    }
  }
}

SCENARIO("Testing NextHopConsistentHash class (all firstcalls), using policy 'consistent_hash'", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("consistent-hash-1");

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 3);
      }
    }

    // Same test procedure as the first scenario but we clear the 'result' struct
    // so that we are making initial requests and simulating that hosts were
    // removed by different transactions.
    //
    // these checks need to be run in sequence so that there are no host status
    // state changes induced by using multiple WHEN() and THEN()
    WHEN("initial requests are made and hosts are unavailable .")
    {
      HttpSM sm;
      ParentResult *result = &sm.t_state.parent_result;
      TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

      THEN("when making requests and taking nodes down.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);

        // first request.
        build_request(20001, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "p1.foo.com") == 0);

        // mark down p1.foo.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // second request
        build_request(20002, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "p2.foo.com") == 0);

        // mark down p2.foo.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // third request
        result->reset();
        build_request(20003, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "s2.bar.com") == 0);

        // mark down s2.bar.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // fourth request
        {
          time_t now = time(nullptr) - 1; ///< make sure down hosts are not retryable
          result->reset();
          build_request(20004, &sm, nullptr, "rabbit.net", nullptr);
          strategy->findNextHop(txnp, nullptr, now);
          REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
          CHECK(strcmp(result->hostname, "s1.bar.com") == 0);
        }

        // mark down s1.bar.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // fifth request
        {
          time_t now = time(nullptr) - 1; ///< make sure down hosts are not retryable
          result->reset();
          build_request(20005, &sm, nullptr, "rabbit.net/asset1", nullptr);
          strategy->findNextHop(txnp, nullptr, now);
          REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
          CHECK(strcmp(result->hostname, "q1.bar.com") == 0);
        }

        // sixth request - wait and p1 should now become available
        {
          time_t now = time(nullptr) + 5;
          result->reset();
          build_request(20006, &sm, nullptr, "rabbit.net", nullptr);
          strategy->findNextHop(txnp, nullptr, now);
          REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
          CHECK(strcmp(result->hostname, "p1.foo.com") == 0);
        }
      }
      // free up request resources.
      br_destroy(sm);
    }
  }
}

SCENARIO("Testing NextHop ignore_self_detect false", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    // load the configuration strtegies.
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("ignore-self-detect-false");

    HostStatus &hs = HostStatus::instance();
    hs.setHostStatus("localhost", TSHostStatus::TS_HOST_STATUS_DOWN, 0, Reason::SELF_DETECT);

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 2);
      }
    }

    WHEN("requests are received.")
    {
      THEN("when making requests to localhost.")
      {
        HttpSM sm;
        ParentResult *result = &sm.t_state.parent_result;
        TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);

        build_request(10001, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        CHECK(result->result == ParentResultType::PARENT_DIRECT);
        CHECK(result->hostname == nullptr);
        br_destroy(sm);
      }
    }
  }
}

SCENARIO("Testing NextHop ignore_self_detect true", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    // load the configuration strtegies.
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("ignore-self-detect-true");

    HostStatus &hs = HostStatus::instance();
    hs.setHostStatus("localhost", TSHostStatus::TS_HOST_STATUS_DOWN, 0, Reason::SELF_DETECT);

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 2);
      }
    }

    WHEN("requests are received.")
    {
      THEN("when making requests to localhost.")
      {
        HttpSM sm;
        ParentResult *result = &sm.t_state.parent_result;
        TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        build_request(10001, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "localhost") == 0);
        CHECK(result->port == 8000);
        br_destroy(sm);
      }
    }
  }
}

SCENARIO("Testing NextHopConsistentHash same host different port markdown", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    // load the configuration strtegies.
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("same-host-different-port");

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 3);
      }
    }

    WHEN("requests are received.")
    {
      THEN("when making requests and taking nodes down.")
      {
        HttpSM sm;
        ParentResult *result = &sm.t_state.parent_result;
        TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);

        // first request.
        build_request(10001, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "localhost") == 0);
        CHECK(result->port == 8000);

        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        build_request(10002, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "localhost") == 0);
        CHECK(result->port == 8002);

        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        build_request(10003, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "localhost") == 0);
        CHECK(result->port == 8004);
        br_destroy(sm);
      }
    }
  }
}

SCENARIO("Testing NextHopConsistentHash hash_string override", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    // load the configuration strtegies.
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("hash-string-override");

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 2);
      }
    }

    WHEN("requests are received.")
    {
      THEN("when making requests and taking nodes down.")
      {
        HttpSM sm;
        ParentResult *result = &sm.t_state.parent_result;
        TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);

        build_request(10001, &sm, nullptr, "rabbit.net", nullptr);
        result->reset();
        strategy->findNextHop(txnp);

        // We happen to know that 'foo.test' will be first if the hostname is the hash
        // and foo.test will be first for the hash 'first' and the bar.test hash 'second'.
        // So, if the hash_string override isn't getting applied, this will fail.
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "bar.test") == 0);
        CHECK(result->port == 80);

        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        build_request(10002, &sm, nullptr, "rabbit.net", nullptr);
        strategy->findNextHop(txnp);

        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "foo.test") == 0);
        CHECK(result->port == 80);
        br_destroy(sm);
      }
    }
  }
}

SCENARIO("Testing NextHopConsistentHash class (alternating rings), using policy 'consistent_hash'", "[NextHopConsistentHash]")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the consistent-hash-tests.yaml config for 'consistent_hash' tests.")
  {
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/consistent-hash-tests.yaml");
    strategy = nhf.strategyInstance("consistent-hash-2");

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 3);
      }
    }

    // making requests and marking down hosts with a config set for alternating ring mode.
    WHEN("requests are made in a config set for alternating rings and hosts are marked down.")
    {
      HttpSM sm;
      ParentResult *result = &sm.t_state.parent_result;
      TSHttpTxn txnp       = reinterpret_cast<TSHttpTxn>(&sm);

      THEN("expect the following results when making requests and marking hosts down.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);

        // first request.
        result->reset();
        build_request(30001, &sm, nullptr, "bunny.net/asset1", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "c2.foo.com") == 0);

        // simulated failure, mark c2 down and retry request
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // second request
        build_request(30002, &sm, nullptr, "bunny.net.net/asset1", nullptr);
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "c3.bar.com") == 0);

        // mark down c3.bar.com
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);

        // third request
        build_request(30003, &sm, nullptr, "bunny.net/asset2", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "c6.bar.com") == 0);

        // just mark it down and retry request
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // fourth request
        build_request(30004, &sm, nullptr, "bunny.net/asset2", nullptr);
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "c1.foo.com") == 0);

        // mark it down
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // fifth request - new request
        build_request(30005, &sm, nullptr, "bunny.net/asset3", nullptr);
        result->reset();
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "c4.bar.com") == 0);

        // mark it down and retry
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // sixth request
        result->reset();
        build_request(30006, &sm, nullptr, "bunny.net/asset3", nullptr);
        strategy->findNextHop(txnp);
        REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
        CHECK(strcmp(result->hostname, "c5.bar.com") == 0);

        // mark it down
        strategy->markNextHop(txnp, result->hostname, result->port, NH_MARK_DOWN);
        // seventh request - new request with all hosts down and go_direct is false.
        {
          time_t now = time(nullptr) - 1; ///< make sure down hosts are not retryable
          result->reset();
          build_request(30007, &sm, nullptr, "bunny.net/asset4", nullptr);
          strategy->findNextHop(txnp, nullptr, now);
          REQUIRE(result->result == ParentResultType::PARENT_FAIL);
          CHECK(result->hostname == nullptr);
        }

        // eighth request - retry after waiting for the retry window to expire.
        {
          time_t now = time(nullptr) + 5;
          result->reset();
          build_request(30008, &sm, nullptr, "bunny.net/asset4", nullptr);
          strategy->findNextHop(txnp, nullptr, now);
          REQUIRE(result->result == ParentResultType::PARENT_SPECIFIED);
          CHECK(strcmp(result->hostname, "c2.foo.com") == 0);
        }
      }
      // free up request resources.
      br_destroy(sm);
    }
  }
}

// jjr
//
SCENARIO("Testing NextHopConsistentHash using a peering ring_mode.")
{
  // We need this to build a HdrHeap object in build_request();
  // No thread setup, forbid use of thread local allocators.
  cmd_disable_pfreelist = true;
  // Get all of the HTTP WKS items populated.
  http_init();

  GIVEN("Loading the peering.yaml config for 'consistent_hash' tests.")
  {
    std::shared_ptr<NextHopSelectionStrategy> strategy;
    NextHopStrategyFactory nhf(TS_SRC_DIR "unit-tests/peering.yaml");
    strategy = nhf.strategyInstance("peering-group-1");

    WHEN("the config is loaded.")
    {
      THEN("then testing consistent hash.")
      {
        REQUIRE(nhf.strategies_loaded == true);
        REQUIRE(strategy != nullptr);
        REQUIRE(strategy->groups == 2);
        REQUIRE(strategy->ring_mode == NH_PEERING_RING);
        REQUIRE(strategy->policy_type == NH_CONSISTENT_HASH);
        for (std::size_t i = 0; i < strategy->host_groups.size(); ++i) {
          for (auto const &elem : strategy->host_groups[i]) {
            bool should_be_self = elem.get()->hostname == "p3.bar.com" && i == 0;
            REQUIRE(elem.get()->self == should_be_self);
          }
        }
      }
    }
  }
}