mirror of
https://github.com/nabbar/golib.git
synced 2025-12-24 11:51:02 +08:00
[file/bandwidth] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases [file/perm] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases - ADD function to parse form "rwx-wxr-x" or "-rwx-w-r-x" - ADD function to ParseFileMode to convert os.FileMode to file.Perm [file/progress] - ADD documentation: add enhanced README and TESTING guidelines - ADD tests: complete test suites with benchmarks, concurrency, and edge cases [ioutils/...] - UPDATE documentation: update enhanced README and TESTING guidelines - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases [logger/...] - UPDATE documentation: update enhanced README and TESTING guidelines - ADD documentation: add enhanced README and TESTING guidelines for sub packages - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases - UPDATE config: remove FileBufferSize from OptionFile (rework hookfile) - UPDATE fields: expose Store function in interface - REWORK hookfile: rework package, use aggregator to allow multi write and single file - FIX hookstderr: fix bug with NonColorable - FIX hookstdout: fix bug with NonColorable - FIX hookwriter: fix bug with NonColorable [network/protocol] - ADD function IsTCP, IsUDP, IsUnixLike to check type of protocol [runner] - FIX typo [socket] - UPDATE documentation: update enhanced README and TESTING guidelines - ADD documentation: add enhanced README and TESTING guidelines for sub packages - UPDATE tests: complete test suites with benchmarks, concurrency, and edge cases - REWORK server: use context compatible io.reader, io.writer, io.closer instead of reader / writer - REWORK server: simplify, optimize server - REMOVE reader, writer type - ADD context: add new interface in root socket interface to expose context interface that extend context, io reader/writer/closer, dediacted function to server (IsConnected, ...)
320 lines
7.4 KiB
Go
320 lines
7.4 KiB
Go
/*
|
|
* MIT License
|
|
*
|
|
* Copyright (c) 2025 Nicolas JUHEL
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
*
|
|
*/
|
|
|
|
package client_test
|
|
|
|
import (
|
|
"os"
|
|
"runtime"
|
|
"time"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
|
|
libptc "github.com/nabbar/golib/network/protocol"
|
|
sckclt "github.com/nabbar/golib/socket/client"
|
|
sckcfg "github.com/nabbar/golib/socket/config"
|
|
)
|
|
|
|
var _ = Describe("Client Factory Creation", func() {
|
|
Context("TCP Client Creation", func() {
|
|
It("should create TCP client successfully", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP,
|
|
Address: "localhost:8080",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
|
|
It("should create TCP4 client successfully", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP4,
|
|
Address: "127.0.0.1:8081",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
|
|
It("should create TCP6 client successfully", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP6,
|
|
Address: "[::1]:8082",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
})
|
|
|
|
Context("UDP Client Creation", func() {
|
|
It("should create UDP client successfully", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUDP,
|
|
Address: "localhost:9000",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
|
|
It("should create UDP4 client successfully", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUDP4,
|
|
Address: "127.0.0.1:9001",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
|
|
It("should create UDP6 client successfully", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUDP6,
|
|
Address: "[::1]:9002",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
})
|
|
|
|
Context("Unix Socket Client Creation", func() {
|
|
// Unix sockets are only available on Linux and Darwin
|
|
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
|
|
It("should create Unix client successfully", func() {
|
|
socketPath := getTestUnixPath()
|
|
defer os.Remove(socketPath)
|
|
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUnix,
|
|
Address: socketPath,
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
|
|
It("should create UnixGram client successfully", func() {
|
|
socketPath := getTestUnixGramPath()
|
|
defer os.Remove(socketPath)
|
|
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUnixGram,
|
|
Address: socketPath,
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
} else {
|
|
It("should return error for Unix client on unsupported platform", func() {
|
|
socketPath := getTestUnixPath()
|
|
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUnix,
|
|
Address: socketPath,
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).To(Equal(sckcfg.ErrInvalidProtocol))
|
|
Expect(cli).To(BeNil())
|
|
})
|
|
|
|
It("should return error for UnixGram client on unsupported platform", func() {
|
|
socketPath := getTestUnixGramPath()
|
|
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkUnixGram,
|
|
Address: socketPath,
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).To(Equal(sckcfg.ErrInvalidProtocol))
|
|
Expect(cli).To(BeNil())
|
|
})
|
|
}
|
|
})
|
|
|
|
Context("Error Handling", func() {
|
|
It("should return error for invalid protocol", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: 255, // Invalid protocol
|
|
Address: "localhost:8080",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(cli).To(BeNil())
|
|
})
|
|
|
|
It("should return error for empty address", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP,
|
|
Address: "", // Empty address
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(cli).To(BeNil())
|
|
})
|
|
|
|
It("should return error for unrecognized protocol value", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkProtocol(255), // Unrecognized protocol
|
|
Address: "localhost:8080",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).To(HaveOccurred())
|
|
Expect(cli).To(BeNil())
|
|
})
|
|
})
|
|
|
|
Context("TLS Configuration", func() {
|
|
It("should create TCP client with TLS enabled", func() {
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP,
|
|
Address: "localhost:8443",
|
|
}
|
|
cfg.TLS.Enabled = true
|
|
cfg.TLS.ServerName = "localhost"
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
})
|
|
})
|
|
|
|
Context("Concurrent Creation", func() {
|
|
It("should allow concurrent client creation", func() {
|
|
done := make(chan bool, 10)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
go func(idx int) {
|
|
defer GinkgoRecover()
|
|
|
|
cfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP,
|
|
Address: "localhost:8080",
|
|
}
|
|
|
|
cli, err := sckclt.New(cfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(cli).ToNot(BeNil())
|
|
|
|
if cli != nil {
|
|
_ = cli.Close()
|
|
}
|
|
|
|
done <- true
|
|
}(i)
|
|
}
|
|
|
|
// Wait for all goroutines to complete
|
|
for i := 0; i < 10; i++ {
|
|
Eventually(done, 5*time.Second).Should(Receive())
|
|
}
|
|
})
|
|
})
|
|
|
|
Context("Multiple Clients", func() {
|
|
It("should allow creating multiple clients with different protocols", func() {
|
|
// TCP client
|
|
tcpCfg := sckcfg.Client{
|
|
Network: libptc.NetworkTCP,
|
|
Address: "localhost:8100",
|
|
}
|
|
tcpCli, err := sckclt.New(tcpCfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(tcpCli).ToNot(BeNil())
|
|
|
|
// UDP client
|
|
udpCfg := sckcfg.Client{
|
|
Network: libptc.NetworkUDP,
|
|
Address: "localhost:9100",
|
|
}
|
|
udpCli, err := sckclt.New(udpCfg, nil)
|
|
Expect(err).ToNot(HaveOccurred())
|
|
Expect(udpCli).ToNot(BeNil())
|
|
|
|
// Cleanup
|
|
if tcpCli != nil {
|
|
_ = tcpCli.Close()
|
|
}
|
|
if udpCli != nil {
|
|
_ = udpCli.Close()
|
|
}
|
|
})
|
|
})
|
|
})
|