wenxiangchao
3 天以前 b544d5cccd76735e034c8055918a50927b437474
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
using System;
using System.IO;
using System.Threading;
using Aging.Infrastructure.Core;
using Aging.ResourceContracts;
using Xunit;
 
namespace Aging.MvpTests
{
    public class FacadeLeaseLifecycleTests : IDisposable
    {
        private readonly string _dbPath;
        private readonly DbLeaseStore _store;
        public FacadeLeaseLifecycleTests()
        {
            _dbPath = Path.Combine(Path.GetTempPath(), "arb_" + Guid.NewGuid().ToString("N") + ".db");
            _store = new DbLeaseStore(_dbPath, "local-main");
        }
        public void Dispose() { try { File.Delete(_dbPath); File.Delete(_dbPath + "-wal"); File.Delete(_dbPath + "-shm"); } catch { } }
 
        private LeaseGrant Grab(string key, int ttl)
            => _store.Acquire(new AcquireRequest
            {
                OwnerId = "f", OwnerType = OwnerTypeV3.Production,
                ResourceKeys = new[] { key }, TtlSeconds = ttl
            }).Grants[0];
 
        [Fact]
        public void Heartbeat_Renew_Extends_Expiry()
        {
            var g = Grab("can/x", 2);
            long before = g.ExpiresUtcMs;
            Thread.Sleep(1200);
            Assert.True(_store.Renew(g, 2));
            Assert.True(_store.ProbeConflict("can/x").ExpiresUtcMs > before);
        }
 
        [Fact]
        public void Expired_Sweep_StaleRenew_False_And_RecoveryBlocked()
        {
            var g = Grab("can/y", 1);
            Thread.Sleep(1500);
            Assert.Equal(1, _store.SweepExpired());
            Assert.False(_store.Renew(g, 5));
            Assert.Null(_store.ProbeConflict("can/y"));
            Assert.Equal(LeasePhase.RecoveryRequired, _store.PhaseOf("can/y"));
        }
    }
}