Skip to content

Commit

Permalink
Merged by Jenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
untangle-bot authored Jan 31, 2024
2 parents 849bd36 + 8b2f1c8 commit 4c1fb48
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 31 deletions.
4 changes: 2 additions & 2 deletions protobuffersrc/Alerts.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ enum AlertType {
CRITICALERROR = 10;
VPN = 11;
CAPTIVEPORTAL = 12;
TRAFFIC = 13;
FIREWALLEVENT = 13;
DYNAMICLISTS = 14;
POLICYMANAGER = 15;
}
Expand All @@ -38,4 +38,4 @@ message Alert {
map<string, string> params = 4;
int64 timestamp = 5;
bool isLoggerAlert = 6;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[{"name":"untangle-node-threat-prevention","allowedState":1},{"name":"untangle-node-sitefilter","allowedState":0},{"name":"untangle-node-geoip","allowedState":0},{"name":"untangle-node-discovery","allowedState":0},{"name":"untangle-node-classd","allowedState":1},{"name":"untangle-node-dynamic-lists","allowedState":0},{"name":"untangle-node-captiveportal","allowedState":0}]
[{"name":"untangle-node-sitefilter","allowedState":0},{"name":"untangle-node-geoip","allowedState":0},{"name":"untangle-node-discovery","allowedState":0},{"name":"untangle-node-classd","allowedState":1},{"name":"untangle-node-dynamic-lists","allowedState":0},{"name":"untangle-node-captiveportal","allowedState":0},{"name":"untangle-node-threat-prevention","allowedState":1}]
34 changes: 17 additions & 17 deletions structs/protocolbuffers/Alerts/Alerts.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions util/cache/cacher/randomreplacementcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@ func (cache *RandomReplacementCache) removeWithoutLock(key string) {
indexToRemove := cache.elements[key].keyIndex

// Order doesn't matter for the keys slice, so delete the fast way.
// Which is just swapping the element to delete with the last element
// Which is swapping the element to delete with the last element
// then ignoring the last element of the slice
cache.keys[indexToRemove] = cache.keys[len(cache.keys)-1]
cache.keys[len(cache.keys)-1] = ""
cache.keys[indexToRemove] = cache.keys[cache.totalElements-1]

// Update index of moved element
cache.elements[key].keyIndex = indexToRemove
movedElementKey := cache.keys[indexToRemove]
cache.elements[movedElementKey].keyIndex = indexToRemove

delete(cache.elements, key)
cache.keys[cache.totalElements-1] = ""
cache.totalElements -= 1
}
// else the key didn't exists in the cache and nothing should be done
}

// Removes an element from the cache.
Expand All @@ -151,8 +151,8 @@ func (cache *RandomReplacementCache) Clear() {
}

// Returns the total elements currently in the cache
func (cache *RandomReplacementCache) GetTotalElements() int {
func (cache *RandomReplacementCache) GetTotalElements() uint {
cache.cacheMutex.RLock()
defer cache.cacheMutex.RUnlock()
return int(cache.totalElements)
return cache.totalElements
}
43 changes: 39 additions & 4 deletions util/cache/cacher/randomreplacementcache_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cacher

import (
"fmt"
"math/rand"
"strconv"
"sync"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -47,7 +50,7 @@ func TestForEachElementMutation(t *testing.T) {
testCache.Put(strconv.Itoa(int(i)), &newVal)
}

assert.Equal(t, testCache.GetTotalElements(), capacity)
assert.Equal(t, testCache.GetTotalElements(), uint(capacity))

mutateElement := func(s string, i interface{}) bool {
deleteElement := false
Expand Down Expand Up @@ -107,11 +110,11 @@ func (suite *RRCacheTestSuite) TestUpdatingCacheValue() {

// Test clearing the cache
func (suite *RRCacheTestSuite) TestClear() {
suite.Equal(int(suite.capacity), suite.cache.GetTotalElements(), "The cache is missing elements. It was not setup properly by SetupTest()")
suite.Equal(suite.capacity, suite.cache.GetTotalElements(), "The cache is missing elements. It was not setup properly by SetupTest()")

suite.cache.Clear()

suite.Equal(0, suite.cache.GetTotalElements(), "The cache was not successfully cleared")
suite.Equal(uint(0), suite.cache.GetTotalElements(), "The cache was not successfully cleared")
}

// Test adding an element when the cache is already at capacity
Expand All @@ -132,9 +135,41 @@ func (suite *RRCacheTestSuite) TestCapacityExceeded() {
suite.NotEqual(keysAfterPut, keysBeforePut)
}

// Test a cache getting hit with random puts/removes
func TestMultiThreaded(t *testing.T) {
var cacheSize uint = 1000
cache := NewRandomReplacementCache(cacheSize, "Multithreaded")
rand.Seed(1)
elementRange := 20000

var wg sync.WaitGroup
for i := 0; i <= 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j <= int(cacheSize)*10; j++ {
element := fmt.Sprintf("%v", rand.Intn(elementRange))
cache.Put(element, element)

if rand.Intn(2) == 0 {
removal := fmt.Sprintf("%v", rand.Intn(elementRange))
cache.Remove(removal)
}
}
}()
}

wg.Wait()

// Check that the value map and keys slice line up
for key, val := range cache.elements {
assert.Equal(t, key, cache.keys[val.keyIndex])
}
}

// Test getting the total elements in the cache
func (suite *RRCacheTestSuite) TestGetTotalElements() {
suite.Equal(int(suite.capacity), suite.cache.GetTotalElements())
suite.Equal(uint(suite.capacity), suite.cache.GetTotalElements())
}

// Test removing elements from the cache
Expand Down

0 comments on commit 4c1fb48

Please sign in to comment.