github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/metrics/json_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:40</date>
    10  //</624450099220582400>
    11  
    12  package metrics
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/json"
    17  	"testing"
    18  )
    19  
    20  func TestRegistryMarshallJSON(t *testing.T) {
    21  	b := &bytes.Buffer{}
    22  	enc := json.NewEncoder(b)
    23  	r := NewRegistry()
    24  	r.Register("counter", NewCounter())
    25  	enc.Encode(r)
    26  	if s := b.String(); "{\"counter\":{\"count\":0}}\n" != s {
    27  		t.Fatalf(s)
    28  	}
    29  }
    30  
    31  func TestRegistryWriteJSONOnce(t *testing.T) {
    32  	r := NewRegistry()
    33  	r.Register("counter", NewCounter())
    34  	b := &bytes.Buffer{}
    35  	WriteJSONOnce(r, b)
    36  	if s := b.String(); s != "{\"counter\":{\"count\":0}}\n" {
    37  		t.Fail()
    38  	}
    39  }
    40