|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 IBM Corp and others. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 6 | + * and Eclipse Distribution License v1.0 which accompany this distribution. |
| 7 | + * |
| 8 | + * The Eclipse Public License is available at |
| 9 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 10 | + * and the Eclipse Distribution License is available at |
| 11 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 12 | + * |
| 13 | + * Contributors: |
| 14 | + * Matt Brittan |
| 15 | + * Daichi Tomaru |
| 16 | + */ |
| 17 | + |
| 18 | +package mqtt |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | +) |
| 24 | + |
| 25 | +func TestGetBackoffSleepTime(t *testing.T) { |
| 26 | + // Test for adding new situation |
| 27 | + controller := newBackoffController() |
| 28 | + if s, c := controller.getBackoffSleepTime(1 * time.Second, 5 * time.Second, "not-exist", 1 * time.Second); !((s == 1 * time.Second) && !c) { |
| 29 | + t.Errorf("When new situation is added, period should be initSleepPeriod and naturally it shouldn't be continual error. s:%d c%t", s, c) |
| 30 | + } |
| 31 | + |
| 32 | + // Test for the continual error in the same situation and suppression of sleep period by maxSleepPeriod |
| 33 | + controller.getBackoffSleepTime(10 * time.Second, 30 * time.Second, "multi", 1 * time.Second) |
| 34 | + if s, c := controller.getBackoffSleepTime(10 * time.Second, 30 * time.Second, "multi", 1 * time.Second); !((s == 20 * time.Second) && c) { |
| 35 | + t.Errorf("When same situation is called again, period should be increased and it should be regarded as a continual error. s:%d c%t", s, c) |
| 36 | + } |
| 37 | + if s, c := controller.getBackoffSleepTime(10 * time.Second, 30 * time.Second, "multi", 1 * time.Second); !((s == 30 * time.Second) && c) { |
| 38 | + t.Errorf("A same situation is called three times. 10 * 2 * 2 = 40 but maxSleepPeriod is 30. So the next period should be 30. s:%d c%t", s, c) |
| 39 | + } |
| 40 | + |
| 41 | + // Test for initialization by elapsed time. |
| 42 | + controller.getBackoffSleepTime(1 * time.Second, 128 * time.Second, "elapsed", 1 * time.Second) |
| 43 | + controller.getBackoffSleepTime(1 * time.Second, 128 * time.Second, "elapsed", 1 * time.Second) |
| 44 | + time.Sleep((1 * 2 + 1 * 2 + 1) * time.Second) |
| 45 | + if s, c := controller.getBackoffSleepTime(1 * time.Second, 128 * time.Second, "elapsed", 1 * time.Second); !((s == 1 * time.Second) && !c) { |
| 46 | + t.Errorf("Initialization should be triggered by elapsed time. s:%d c%t", s, c) |
| 47 | + } |
| 48 | + |
| 49 | + // Test when initial and max period is same. |
| 50 | + controller.getBackoffSleepTime(1 * time.Second, 1 * time.Second, "same", 1 * time.Second) |
| 51 | + if s, c := controller.getBackoffSleepTime(1 * time.Second, 1 * time.Second, "same", 1 * time.Second); !((s == 1 * time.Second) && c) { |
| 52 | + t.Errorf("Sleep time should be always 1. s:%d c%t", s, c) |
| 53 | + } |
| 54 | +} |
0 commit comments