diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
commit | 3e45412327a2654a77944249962b3652e6142299 (patch) | |
tree | bc3bf69452afa055423cbe0c5cfa8ca357df6ccf /src/pkg/expvar | |
parent | c533680039762cacbc37db8dc7eed074c3e497be (diff) | |
download | golang-upstream/2011.01.12.tar.gz |
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'src/pkg/expvar')
-rw-r--r-- | src/pkg/expvar/Makefile | 2 | ||||
-rw-r--r-- | src/pkg/expvar/expvar.go | 26 | ||||
-rw-r--r-- | src/pkg/expvar/expvar_test.go | 24 |
3 files changed, 39 insertions, 13 deletions
diff --git a/src/pkg/expvar/Makefile b/src/pkg/expvar/Makefile index 544891739..5619630d1 100644 --- a/src/pkg/expvar/Makefile +++ b/src/pkg/expvar/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../Make.$(GOARCH) +include ../../Make.inc TARG=expvar GOFILES=\ diff --git a/src/pkg/expvar/expvar.go b/src/pkg/expvar/expvar.go index 4017027b7..fb20b25b2 100644 --- a/src/pkg/expvar/expvar.go +++ b/src/pkg/expvar/expvar.go @@ -6,6 +6,8 @@ // such as operation counters in servers. It exposes these variables via // HTTP at /debug/vars in JSON format. // +// Operations to set or modify these public variables are atomic. +// // In addition to adding the HTTP handler, this package registers the // following variables: // @@ -50,6 +52,12 @@ func (v *Int) Add(delta int64) { v.i += delta } +func (v *Int) Set(value int64) { + v.mu.Lock() + defer v.mu.Unlock() + v.i = value +} + // Map is a string-to-Var map variable, and satisfies the Var interface. type Map struct { m map[string]Var @@ -144,7 +152,7 @@ func (v IntFunc) String() string { return strconv.Itoa64(v()) } // The function will be called each time the Var is evaluated. type StringFunc func() string -func (f StringFunc) String() string { return f() } +func (f StringFunc) String() string { return strconv.Quote(f()) } // All published variables. @@ -153,12 +161,12 @@ var mutex sync.Mutex // Publish declares an named exported variable. This should be called from a // package's init function when it creates its Vars. If the name is already -// registered then this will log.Crash. +// registered then this will log.Panic. func Publish(name string, v Var) { mutex.Lock() defer mutex.Unlock() if _, existing := vars[name]; existing { - log.Crash("Reuse of exported var name:", name) + log.Panicln("Reuse of exported var name:", name) } vars[name] = v } @@ -210,18 +218,18 @@ func Iter() <-chan KeyValue { return c } -func expvarHandler(c *http.Conn, req *http.Request) { - c.SetHeader("content-type", "application/json; charset=utf-8") - fmt.Fprintf(c, "{\n") +func expvarHandler(w http.ResponseWriter, r *http.Request) { + w.SetHeader("content-type", "application/json; charset=utf-8") + fmt.Fprintf(w, "{\n") first := true for name, value := range vars { if !first { - fmt.Fprintf(c, ",\n") + fmt.Fprintf(w, ",\n") } first = false - fmt.Fprintf(c, "%q: %s", name, value) + fmt.Fprintf(w, "%q: %s", name, value) } - fmt.Fprintf(c, "\n}\n") + fmt.Fprintf(w, "\n}\n") } func memstats() string { diff --git a/src/pkg/expvar/expvar_test.go b/src/pkg/expvar/expvar_test.go index 98cd9c2ea..009f24d1a 100644 --- a/src/pkg/expvar/expvar_test.go +++ b/src/pkg/expvar/expvar_test.go @@ -27,6 +27,11 @@ func TestInt(t *testing.T) { if s := reqs.String(); s != "4" { t.Errorf("reqs.String() = %q, want \"4\"", s) } + + reqs.Set(-2) + if reqs.i != -2 { + t.Errorf("reqs.i = %v, want -2", reqs.i) + } } func TestString(t *testing.T) { @@ -76,13 +81,13 @@ func TestMapCounter(t *testing.T) { t.Error("red.Kind() is not a number.") } if x != 3 { - t.Error("red = %v, want 3", x) + t.Errorf("red = %v, want 3", x) } } func TestIntFunc(t *testing.T) { - x := int(4) - ix := IntFunc(func() int64 { return int64(x) }) + x := int64(4) + ix := IntFunc(func() int64 { return x }) if s := ix.String(); s != "4" { t.Errorf("ix.String() = %v, want 4", s) } @@ -92,3 +97,16 @@ func TestIntFunc(t *testing.T) { t.Errorf("ix.String() = %v, want 5", s) } } + +func TestStringFunc(t *testing.T) { + x := "hello" + sx := StringFunc(func() string { return x }) + if s, exp := sx.String(), `"hello"`; s != exp { + t.Errorf(`sx.String() = %q, want %q`, s, exp) + } + + x = "goodbye" + if s, exp := sx.String(), `"goodbye"`; s != exp { + t.Errorf(`sx.String() = %q, want %q`, s, exp) + } +} |