func (*suite) TestWriteMessageLogsRequests(c *C) {
	codec := jsoncodec.New(&testConn{})
	h := rpc.Header{
		RequestId: 1,
		Type:      "foo",
		Id:        "id",
		Request:   "frob",
	}

	// Check that logging is off by default
	err := codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, "")

	// Check that we see a log message when we switch logging on.
	codec.SetLogging(true)
	err = codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	msg := `{"RequestId":1,"Type":"foo","Id":"id","Request":"frob","Params":{"X":"param"}}`
	c.Assert(c.GetTestLog(), Matches, `.*DEBUG juju rpc/jsoncodec: -> `+regexp.QuoteMeta(msg)+`\n`)

	// Check that we can switch it off again
	codec.SetLogging(false)
	err = codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, `.*DEBUG juju rpc/jsoncodec: -> `+regexp.QuoteMeta(msg)+`\n`)
}
func (*suite) TestWrite(c *C) {
	for i, test := range writeTests {
		c.Logf("test %d", i)
		var conn testConn
		codec := jsoncodec.New(&conn)
		err := codec.WriteMessage(test.hdr, test.body)
		c.Assert(err, IsNil)
		c.Assert(conn.writeMsgs, HasLen, 1)

		assertJSONEqual(c, conn.writeMsgs[0], test.expect)
	}
}
func (*suite) TestErrorAfterClose(c *C) {
	conn := &testConn{
		err: errors.New("some error"),
	}
	codec := jsoncodec.New(conn)
	var hdr rpc.Header
	err := codec.ReadHeader(&hdr)
	c.Assert(err, ErrorMatches, "error receiving message: some error")

	err = codec.Close()
	c.Assert(err, IsNil)
	c.Assert(conn.closed, Equals, true)

	err = codec.ReadHeader(&hdr)
	c.Assert(err, Equals, io.EOF)
}
func (*suite) TestConcurrentSetLoggingAndRead(c *C) {
	// If log messages are not set atomically, this
	// test will fail when run under the race detector.
	msg := `{"RequestId":1,"Type": "foo","Id": "id","Request":"frob","Params":{"X":"param"}}`
	codec := jsoncodec.New(&testConn{
		readMsgs: []string{msg, msg, msg},
	})
	done := make(chan struct{})
	go func() {
		codec.SetLogging(true)
		done <- struct{}{}
	}()
	var h rpc.Header
	err := codec.ReadHeader(&h)
	c.Assert(err, IsNil)
	<-done
}
func (*suite) TestConcurrentSetLoggingAndWrite(c *C) {
	// If log messages are not set atomically, this
	// test will fail when run under the race detector.
	codec := jsoncodec.New(&testConn{})
	done := make(chan struct{})
	go func() {
		codec.SetLogging(true)
		done <- struct{}{}
	}()
	h := rpc.Header{
		RequestId: 1,
		Type:      "foo",
		Id:        "id",
		Request:   "frob",
	}
	err := codec.WriteMessage(&h, value{X: "param"})
	c.Assert(err, IsNil)
	<-done
}
func (*suite) TestRead(c *C) {
	for i, test := range readTests {
		c.Logf("test %d", i)
		codec := jsoncodec.New(&testConn{
			readMsgs: []string{test.msg},
		})
		var hdr rpc.Header
		err := codec.ReadHeader(&hdr)
		c.Assert(err, IsNil)
		c.Assert(hdr, DeepEquals, test.expectHdr)

		c.Assert(hdr.IsRequest(), Equals, test.expectHdr.IsRequest())

		body := reflect.New(reflect.ValueOf(test.expectBody).Type().Elem()).Interface()
		err = codec.ReadBody(body, test.expectHdr.IsRequest())
		c.Assert(err, IsNil)
		c.Assert(body, DeepEquals, test.expectBody)

		err = codec.ReadHeader(&hdr)
		c.Assert(err, Equals, io.EOF)
	}
}
func (*suite) TestReadHeaderLogsRequests(c *C) {
	msg := `{"RequestId":1,"Type": "foo","Id": "id","Request":"frob","Params":{"X":"param"}}`
	codec := jsoncodec.New(&testConn{
		readMsgs: []string{msg, msg, msg},
	})
	// Check that logging is off by default
	var h rpc.Header
	err := codec.ReadHeader(&h)
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, "")

	// Check that we see a log message when we switch logging on.
	codec.SetLogging(true)
	err = codec.ReadHeader(&h)
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, ".*DEBUG juju rpc/jsoncodec: <- "+regexp.QuoteMeta(msg)+`\n`)

	// Check that we can switch it off again
	codec.SetLogging(false)
	err = codec.ReadHeader(&h)
	c.Assert(err, IsNil)
	c.Assert(c.GetTestLog(), Matches, ".*DEBUG juju rpc/jsoncodec: <- "+regexp.QuoteMeta(msg)+`\n`)
}