|
| 1 | +package org.stellar.sdk |
| 2 | + |
| 3 | +import io.kotest.assertions.throwables.shouldThrow |
| 4 | +import io.kotest.core.spec.style.FunSpec |
| 5 | +import io.kotest.matchers.longs.shouldBeInRange |
| 6 | +import io.kotest.matchers.shouldBe |
| 7 | + |
| 8 | +class TimeBoundsTest : |
| 9 | + FunSpec({ |
| 10 | + test("should throw IllegalArgumentException for negative minTime") { |
| 11 | + val exception = shouldThrow<IllegalArgumentException> { TimeBounds(-1, 300) } |
| 12 | + exception.message shouldBe "minTime must be between 0 and 2^64-1" |
| 13 | + } |
| 14 | + |
| 15 | + test("should throw IllegalArgumentException for negative maxTime") { |
| 16 | + val exception = shouldThrow<IllegalArgumentException> { TimeBounds(1, -300) } |
| 17 | + exception.message shouldBe "maxTime must be between 0 and 2^64-1" |
| 18 | + } |
| 19 | + |
| 20 | + test("should throw IllegalArgumentException when minTime greater than maxTime") { |
| 21 | + val exception = shouldThrow<IllegalArgumentException> { TimeBounds(300, 1) } |
| 22 | + exception.message shouldBe "minTime must be <= maxTime" |
| 23 | + } |
| 24 | + |
| 25 | + test("should create TimeBounds with infinite timeout (maxTime = 0)") { |
| 26 | + val timeBounds = TimeBounds(300, 0) |
| 27 | + timeBounds.minTime.toLong() shouldBe 300 |
| 28 | + timeBounds.maxTime.toLong() shouldBe 0 |
| 29 | + TimeBounds.fromXdr(timeBounds.toXdr()) shouldBe timeBounds |
| 30 | + } |
| 31 | + |
| 32 | + test("should create TimeBounds with both times zero") { |
| 33 | + val timeBounds = TimeBounds(0, 0) |
| 34 | + timeBounds.minTime.toLong() shouldBe 0 |
| 35 | + timeBounds.maxTime.toLong() shouldBe 0 |
| 36 | + TimeBounds.fromXdr(timeBounds.toXdr()) shouldBe timeBounds |
| 37 | + } |
| 38 | + |
| 39 | + test("should create TimeBounds with valid range") { |
| 40 | + val timeBounds = TimeBounds(1, 300) |
| 41 | + timeBounds.minTime.toLong() shouldBe 1 |
| 42 | + timeBounds.maxTime.toLong() shouldBe 300 |
| 43 | + TimeBounds.fromXdr(timeBounds.toXdr()) shouldBe timeBounds |
| 44 | + } |
| 45 | + |
| 46 | + test("should create TimeBounds with equal min and max time") { |
| 47 | + val timeBounds = TimeBounds(300, 300) |
| 48 | + timeBounds.minTime.toLong() shouldBe 300 |
| 49 | + timeBounds.maxTime.toLong() shouldBe 300 |
| 50 | + TimeBounds.fromXdr(timeBounds.toXdr()) shouldBe timeBounds |
| 51 | + } |
| 52 | + |
| 53 | + test("should create TimeBounds with timeout using expiresAfter") { |
| 54 | + val timeout = 300L |
| 55 | + val timeBounds = TimeBounds.expiresAfter(timeout) |
| 56 | + val now = System.currentTimeMillis() / 1000L |
| 57 | + |
| 58 | + timeBounds.minTime.toLong() shouldBe 0 |
| 59 | + val actualMaxTime = timeBounds.maxTime.toLong() |
| 60 | + actualMaxTime shouldBeInRange (now + timeout - 1)..(now + timeout + 1) |
| 61 | + } |
| 62 | + |
| 63 | + test("should handle large time values") { |
| 64 | + val largeTime = Long.MAX_VALUE - 1 |
| 65 | + val timeBounds = TimeBounds(0, largeTime) |
| 66 | + timeBounds.minTime.toLong() shouldBe 0 |
| 67 | + timeBounds.maxTime.toLong() shouldBe largeTime |
| 68 | + TimeBounds.fromXdr(timeBounds.toXdr()) shouldBe timeBounds |
| 69 | + } |
| 70 | + |
| 71 | + test("should handle maximum valid long values") { |
| 72 | + val maxTime = Long.MAX_VALUE |
| 73 | + val timeBounds = TimeBounds(maxTime, maxTime) |
| 74 | + timeBounds.minTime.toLong() shouldBe maxTime |
| 75 | + timeBounds.maxTime.toLong() shouldBe maxTime |
| 76 | + TimeBounds.fromXdr(timeBounds.toXdr()) shouldBe timeBounds |
| 77 | + } |
| 78 | + }) |
0 commit comments