Skip to content

Instantly share code, notes, and snippets.

@cwndrws
Created September 25, 2014 16:01
Show Gist options
  • Save cwndrws/4074c299c7cf672a9136 to your computer and use it in GitHub Desktop.
Save cwndrws/4074c299c7cf672a9136 to your computer and use it in GitHub Desktop.
test for memory problems in gob
package main
import (
"encoding/gob"
"log"
"net"
"net/rpc"
"time"
)
type chanCon struct {
con chan []byte
}
type listener struct {
con *chanCon
}
type Server struct{}
type BigThing struct {
One string
Two string
Three string
Four string
Five string
}
type Response struct {
Data BigThing
}
func NewResponse() *Response {
return &Response{
Data: BigThing{},
}
}
func NewBigThing() *BigThing {
return &BigThing{
One: "fadslkjasd;flkjas;dlkfjas;ldkfjalskdjfaskdjhflakjshdf",
Two: "asdlfkjhasljdhfkjhkjhsdaflkjahsdflkjhasdfljkhasdkfhjlkasjdhf",
Three: "fltheakjlasdfjhthtalksjdf;laksjdffdaskjhasdkfjhasdkfjh",
Four: "asdkjfhasdfhasdflkjasdlkfjhsalfjhlaksjdhflkasjdhflkajshdlfjhasdf",
Five: "fdasjkhfaskdjhflksajhfoiueryojkhdnv,m97324h2rjnf,jdhaflshjasd",
}
}
func (c *chanCon) Read(b []byte) (int, error) {
recv := <-c.con
counter := 0
log.Printf("RECV len: %d\n", len(recv))
if len(recv) < len(b) {
for i, _ := range recv {
b[i] = recv[i]
counter++
}
return counter, nil
}
for i, _ := range b {
b[i] = recv[i]
counter++
}
log.Printf("b: %s\n", string(b))
return counter, nil
}
func (c *chanCon) Write(b []byte) (int, error) {
log.Printf("Got To Write\n")
len := len(b)
c.con <- b
return len, nil
}
func (c *chanCon) Close() error {
return nil
}
func (c *chanCon) LocalAddr() net.Addr {
return nil
}
func (c *chanCon) RemoteAddr() net.Addr {
return nil
}
func (c *chanCon) SetDeadline(t time.Time) error {
return nil
}
func (c *chanCon) SetReadDeadline(t time.Time) error {
return nil
}
func (c *chanCon) SetWriteDeadline(t time.Time) error {
return nil
}
func (l *listener) Accept() (net.Conn, error) {
return l.con, nil
}
func (l *listener) Close() error {
return nil
}
func (l *listener) Addr() net.Addr {
return nil
}
func (s *Server) DoThing(args BigThing, reply *Response) error {
log.Printf("Got To DoThing\n")
reply.Data = args
return nil
}
func Do() {
conn := &chanCon{
con: make(chan []byte),
}
cli := rpc.NewClient(conn)
l := &listener{
con: conn,
}
newServ := &Server{}
s := rpc.NewServer()
s.Register(newServ)
go s.Accept(l)
args := NewBigThing()
reply := NewResponse()
if err := cli.Call("Server.DoThing", args, reply); err != nil {
log.Printf("Error doing the thing: %v\n", err)
}
}
func init() {
gob.RegisterName("Response", NewResponse())
}
func main() {
Do()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment