2020-09-15 14:44:38 +08:00
|
|
|
package schedulingconfigs
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"testing"
|
2024-07-27 13:29:26 +08:00
|
|
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
2020-09-13 19:27:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestStickyScheduling_NextArgument(t *testing.T) {
|
|
|
|
|
s := &StickyScheduling{}
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "a",
|
|
|
|
|
Weight: 1,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "b",
|
|
|
|
|
Weight: 2,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "c",
|
|
|
|
|
Weight: 3,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "d",
|
|
|
|
|
Weight: 6,
|
|
|
|
|
})
|
|
|
|
|
s.Start()
|
|
|
|
|
|
|
|
|
|
t.Log(s.mapping)
|
|
|
|
|
|
2023-08-08 18:50:12 +08:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://www.example.com/?origin=c", nil)
|
2020-09-13 19:27:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
options := maps.Map{
|
|
|
|
|
"type": "argument",
|
2020-09-15 14:44:38 +08:00
|
|
|
"param": "origin",
|
2020-09-13 19:27:47 +08:00
|
|
|
}
|
|
|
|
|
call := shared.NewRequestCall()
|
|
|
|
|
call.Request = req
|
|
|
|
|
call.Options = options
|
|
|
|
|
t.Log(s.Next(call))
|
|
|
|
|
t.Log(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStickyScheduling_NextCookie(t *testing.T) {
|
|
|
|
|
s := &StickyScheduling{}
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "a",
|
|
|
|
|
Weight: 1,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "b",
|
|
|
|
|
Weight: 2,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "c",
|
|
|
|
|
Weight: 3,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "d",
|
|
|
|
|
Weight: 6,
|
|
|
|
|
})
|
|
|
|
|
s.Start()
|
|
|
|
|
|
|
|
|
|
t.Log(s.mapping)
|
|
|
|
|
|
2023-08-08 18:50:12 +08:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://www.example.com/?origin=c", nil)
|
2020-09-13 19:27:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.AddCookie(&http.Cookie{
|
2020-09-15 14:44:38 +08:00
|
|
|
Name: "origin",
|
2020-09-13 19:27:47 +08:00
|
|
|
Value: "c",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
options := maps.Map{
|
|
|
|
|
"type": "cookie",
|
2020-09-15 14:44:38 +08:00
|
|
|
"param": "origin",
|
2020-09-13 19:27:47 +08:00
|
|
|
}
|
|
|
|
|
call := shared.NewRequestCall()
|
|
|
|
|
call.Request = req
|
|
|
|
|
call.Options = options
|
|
|
|
|
t.Log(s.Next(call))
|
|
|
|
|
t.Log(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStickyScheduling_NextHeader(t *testing.T) {
|
|
|
|
|
s := &StickyScheduling{}
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "a",
|
|
|
|
|
Weight: 1,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "b",
|
|
|
|
|
Weight: 2,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "c",
|
|
|
|
|
Weight: 3,
|
|
|
|
|
})
|
|
|
|
|
s.Add(&TestCandidate{
|
|
|
|
|
Name: "d",
|
|
|
|
|
Weight: 6,
|
|
|
|
|
})
|
|
|
|
|
s.Start()
|
|
|
|
|
|
|
|
|
|
t.Log(s.mapping)
|
|
|
|
|
|
2023-08-08 18:50:12 +08:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://www.example.com/?origin=c", nil)
|
2020-09-13 19:27:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2020-09-15 14:44:38 +08:00
|
|
|
req.Header.Set("origin", "c")
|
2020-09-13 19:27:47 +08:00
|
|
|
|
|
|
|
|
options := maps.Map{
|
|
|
|
|
"type": "header",
|
2020-09-15 14:44:38 +08:00
|
|
|
"param": "origin",
|
2020-09-13 19:27:47 +08:00
|
|
|
}
|
|
|
|
|
call := shared.NewRequestCall()
|
|
|
|
|
call.Request = req
|
|
|
|
|
call.Options = options
|
|
|
|
|
t.Log(s.Next(call))
|
|
|
|
|
t.Log(options)
|
|
|
|
|
}
|