From a189e0ad46aaca27417ed8bb301c2b1abb496310 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Sun, 11 Oct 2020 22:08:03 +0200 Subject: [PATCH] Implement packetcache.Last. --- packetcache/packetcache.go | 14 ++++++++++++++ packetcache/packetcache_test.go | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packetcache/packetcache.go b/packetcache/packetcache.go index 7b83d46..a21f2e6 100644 --- a/packetcache/packetcache.go +++ b/packetcache/packetcache.go @@ -388,6 +388,20 @@ func (cache *Cache) Get(seqno uint16, result []byte) uint16 { return 0 } +func (cache *Cache) Last() (bool, uint16, uint32) { + cache.mu.Lock() + defer cache.mu.Unlock() + if !cache.lastValid { + return false, 0, 0 + } + buf := make([]byte, BufSize) + len, ts, _ := get(cache.last, cache.entries, buf) + if len == 0 { + return false, 0, 0 + } + return true, cache.last, ts +} + // GetAt retrieves a packet from the cache assuming it is at the given index. func (cache *Cache) GetAt(seqno uint16, index uint16, result []byte) uint16 { cache.mu.Lock() diff --git a/packetcache/packetcache_test.go b/packetcache/packetcache_test.go index 756ae83..c378370 100644 --- a/packetcache/packetcache_test.go +++ b/packetcache/packetcache_test.go @@ -20,8 +20,23 @@ func TestCache(t *testing.T) { buf1 := randomBuf() buf2 := randomBuf() cache := New(16) - _, i1 := cache.Store(13, 0, false, false, buf1) - _, i2 := cache.Store(17, 0, false, false, buf2) + + found, _, _ := cache.Last() + if found { + t.Errorf("Found in empty cache") + } + + _, i1 := cache.Store(13, 42, false, false, buf1) + _, i2 := cache.Store(17, 42, false, false, buf2) + + found, seqno, ts := cache.Last() + if !found { + t.Errorf("Not found") + } + if seqno != 17 || ts != 42 { + t.Errorf("Expected %v, %v, got %v, %v", + 17, 42, seqno, ts) + } buf := make([]byte, BufSize)