Browse Source

(#86) Add UnregisterAll to Registry and StandardRegistry

This new interface method and its standard implementation will make it
easier to test libraries that depend on go-metrics and assume the
default registry (which is analogous to the relationship between expvar
and http.DefaultServeMux).
Richard Crowley 11 năm trước cách đây
mục cha
commit
dee209f245
1 tập tin đã thay đổi với 12 bổ sung0 xóa
  1. 12 0
      registry.go

+ 12 - 0
registry.go

@@ -41,6 +41,9 @@ type Registry interface {
 
 	// Unregister the metric with the given name.
 	Unregister(string)
+
+	// Unregister all metrics.  (Mostly for testing.)
+	UnregisterAll()
 }
 
 // The standard implementation of a Registry is a mutex-protected map
@@ -112,6 +115,15 @@ func (r *StandardRegistry) Unregister(name string) {
 	delete(r.metrics, name)
 }
 
+// Unregister all metrics.  (Mostly for testing.)
+func (r *StandardRegistry) UnregisterAll() {
+	r.mutex.Lock()
+	defer r.mutex.Unlock()
+	for name, _ := range r.metrics {
+		delete(r.metrics, name)
+	}
+}
+
 func (r *StandardRegistry) register(name string, i interface{}) error {
 	if _, ok := r.metrics[name]; ok {
 		return DuplicateMetric(name)