Commit a189e0ad authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

Implement packetcache.Last.

parent e6bf9338
......@@ -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()
......
......@@ -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)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment