-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package unix | ||
|
||
import ( | ||
"net" | ||
"os" | ||
"testing" | ||
|
||
"github.com/andeya/erpc/v7" | ||
"github.com/andeya/goutil" | ||
) | ||
|
||
//go:generate go test -v -c -o "${GOPACKAGE}_client" $GOFILE | ||
|
||
func TestClient(t *testing.T) { | ||
if goutil.IsGoTest() { | ||
t.Log("skip test in go test") | ||
return | ||
} | ||
defer erpc.SetLoggerLevel("ERROR")() | ||
|
||
cli := erpc.NewPeer( | ||
erpc.PeerConfig{ | ||
Network: "unix", | ||
LocalPort: 9091, | ||
}, | ||
&erpc.PluginImpl{ | ||
PluginName: "clean-local-unix-file", | ||
OnPreDial: func(localAddr net.Addr, remoteAddr string) (stat *erpc.Status) { | ||
addr := localAddr.String() | ||
if _, err := os.Stat(addr); err == nil { | ||
if err := os.RemoveAll(addr); err != nil { | ||
return erpc.NewStatusByCodeText(erpc.CodeDialFailed, err, false) | ||
} | ||
} | ||
return nil | ||
}, | ||
}, | ||
) | ||
defer cli.Close() | ||
|
||
sess, stat := cli.Dial("0.0.0.0:9090") | ||
if !stat.OK() { | ||
erpc.Fatalf("%v", stat) | ||
} | ||
|
||
var result string | ||
stat = sess.Call( | ||
"/echo/parrot", | ||
"this is request", | ||
&result, | ||
).Status() | ||
|
||
if !stat.OK() { | ||
erpc.Fatalf("%v", stat) | ||
} | ||
erpc.Printf("result: %s", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package unix | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/andeya/erpc/v7" | ||
"github.com/andeya/goutil" | ||
) | ||
|
||
//go:generate go test -v -c -o "${GOPACKAGE}_server" $GOFILE | ||
|
||
func TestServer(t *testing.T) { | ||
if goutil.IsGoTest() { | ||
t.Log("skip test in go test") | ||
return | ||
} | ||
|
||
defer erpc.FlushLogger() | ||
srv := erpc.NewPeer(erpc.PeerConfig{ | ||
CountTime: true, | ||
ListenPort: 9090, | ||
Network: "unix", | ||
}, &erpc.PluginImpl{ | ||
PluginName: "clean-listen-unix-file", | ||
OnPreNewPeer: func(config *erpc.PeerConfig, _ *erpc.PluginContainer) error { | ||
socketFile := config.ListenAddr().String() | ||
if _, err := os.Stat(socketFile); err == nil { | ||
if err := os.RemoveAll(socketFile); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}, | ||
}) | ||
srv.RouteCall(&Echo{}) | ||
srv.ListenAndServe() | ||
} | ||
|
||
type Echo struct { | ||
erpc.CallCtx | ||
} | ||
|
||
func (echo *Echo) Parrot(arg *string) (string, *erpc.Status) { | ||
return *arg, nil | ||
} |