Skip to content

Commit

Permalink
feat(chore): add caddy multi storers unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darkweak committed Aug 20, 2023
1 parent c43b4ce commit 662a8b7
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 26 deletions.
54 changes: 28 additions & 26 deletions plugins/caddy/httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,35 +244,37 @@ func (s *SouinCaddyMiddleware) Provision(ctx caddy.Context) error {
s.SouinBaseHandler = bh
dc := s.SouinBaseHandler.Configuration.GetDefaultCache()
if dc.GetDistributed() {
if eo, ok := s.SouinBaseHandler.Storer.(*storage.EmbeddedOlric); ok {
name := fmt.Sprintf("0.0.0.0:%d", config.DefaultPort)
if dc.GetOlric().Configuration != nil {
oc := dc.GetOlric().Configuration.(*config.Config)
name = fmt.Sprintf("%s:%d", oc.BindAddr, oc.BindPort)
} else if dc.GetOlric().Path != "" {
name = dc.GetOlric().Path
}

key := "Embedded-" + name
v, _ := up.LoadOrStore(stored_providers_key, newStorageProvider())
v.(*storage_providers).Add(key)
for _, currentStorer := range s.SouinBaseHandler.Storers {
if eo, ok := currentStorer.(*storage.EmbeddedOlric); ok {
name := fmt.Sprintf("0.0.0.0:%d", config.DefaultPort)
if dc.GetOlric().Configuration != nil {
oc := dc.GetOlric().Configuration.(*config.Config)
name = fmt.Sprintf("%s:%d", oc.BindAddr, oc.BindPort)
} else if dc.GetOlric().Path != "" {
name = dc.GetOlric().Path
}

if eo.GetDM() == nil {
v, l, e := up.LoadOrNew(key, func() (caddy.Destructor, error) {
s.logger.Sugar().Debug("Create a new olric instance.")
eo, err := storage.EmbeddedOlricConnectionFactory(s.Configuration)
if eo != nil {
return eo.(*storage.EmbeddedOlric), err
key := "Embedded-" + name
v, _ := up.LoadOrStore(stored_providers_key, newStorageProvider())
v.(*storage_providers).Add(key)

if eo.GetDM() == nil {
v, l, e := up.LoadOrNew(key, func() (caddy.Destructor, error) {
s.logger.Sugar().Debug("Create a new olric instance.")
eo, err := storage.EmbeddedOlricConnectionFactory(s.Configuration)
if eo != nil {
return eo.(*storage.EmbeddedOlric), err
}
return nil, err
})

if l && e == nil {
s.SouinBaseHandler.Storer = v.(storage.Storer)
}
return nil, err
})

if l && e == nil {
s.SouinBaseHandler.Storer = v.(storage.Storer)
} else {
s.logger.Sugar().Debug("Store the olric instance.")
_, _ = up.LoadOrStore(key, s.SouinBaseHandler.SurrogateKeyStorer)
}
} else {
s.logger.Sugar().Debug("Store the olric instance.")
_, _ = up.LoadOrStore(key, s.SouinBaseHandler.SurrogateKeyStorer)
}
}
}
Expand Down
82 changes: 82 additions & 0 deletions plugins/caddy/httpcache_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package httpcache

import (
"fmt"
"net/http"
"os"
"path"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -289,6 +293,84 @@ func TestNotHandledRoute(t *testing.T) {
}
}

func TestMultiProvider(t *testing.T) {
var wg sync.WaitGroup
var responses []*http.Response

for i := 0; i < 4; i++ {
wg.Add(1)

go func(tt *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
{
admin localhost:2999
http_port 9080
https_port 9443
cache {
nuts {
path ./souin-nuts
}
ttl 1000s
storers badger nuts
}
}
localhost:9080 {
route /multi-storage {
cache
respond "Hello, multi-storage!"
}
}`, "caddyfile")

resp, _ := tester.AssertGetResponse(`http://localhost:9080/multi-storage`, 200, "Hello, multi-storage!")
responses = append(responses, resp)
resp, _ = tester.AssertGetResponse(`http://localhost:9080/multi-storage`, 200, "Hello, multi-storage!")
responses = append(responses, resp)
wg.Done()
}(t)

wg.Wait()
time.Sleep(time.Second)
}

resp1 := responses[0]
if resp1.Header.Get("Cache-Status") != "Souin; fwd=uri-miss; stored; key=GET-http-localhost:9080-/multi-storage" {
t.Errorf("unexpected resp1 Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}
if resp1.Header.Get("Age") != "" {
t.Errorf("unexpected resp1 Age header %v", resp1.Header.Get("Age"))
}
resp1 = responses[1]
if resp1.Header.Get("Cache-Status") != "Souin; hit; ttl=999; key=GET-http-localhost:9080-/multi-storage" {
t.Errorf("unexpected resp3 Cache-Status header %v", resp1.Header.Get("Cache-Status"))
}
if resp1.Header.Get("Age") != "1" {
t.Errorf("unexpected resp1 Age header %v", resp1.Header.Get("Age"))
}

for i := 0; i < (len(responses)/2)-1; i++ {
currentIteration := 2 + (i * 2)
resp := responses[currentIteration]
if resp.Header.Get("Cache-Status") != "Souin; hit; ttl="+fmt.Sprint(998-i)+"; key=GET-http-localhost:9080-/multi-storage" {
t.Errorf("unexpected resp%d Cache-Status header %v", currentIteration, resp.Header.Get("Cache-Status"))
}
if resp.Header.Get("Age") != fmt.Sprint(2+i) {
t.Errorf("unexpected resp%d Age header %v", currentIteration, resp.Header.Get("Age"))
}
currentIteration++
resp = responses[currentIteration]
if resp.Header.Get("Cache-Status") != "Souin; hit; ttl="+fmt.Sprint(998-i)+"; key=GET-http-localhost:9080-/multi-storage" {
t.Errorf("unexpected resp%d Cache-Status header %v", currentIteration, resp.Header.Get("Cache-Status"))
}
if resp.Header.Get("Age") != fmt.Sprint(2+i) {
t.Errorf("unexpected resp%d Age header %v", currentIteration, resp.Header.Get("Age"))
}
}

os.RemoveAll(path.Join(".", "souin-nuts"))
TestMinimal(t)
}

func TestAuthenticatedRoute(t *testing.T) {
tester := caddytest.NewTester(t)
tester.InitServer(`
Expand Down

0 comments on commit 662a8b7

Please sign in to comment.