Ben Johnson 12 年之前
父節點
當前提交
228754a99c
共有 3 個文件被更改,包括 85 次插入3 次删除
  1. 44 0
      mod/lock/handler.go
  2. 35 0
      mod/lock/handler_test.go
  3. 6 3
      mod/mod.go

+ 44 - 0
mod/lock/handler.go

@@ -0,0 +1,44 @@
+package lock
+
+import (
+	"bytes"
+	"net/http"
+	"os"
+	"path"
+	"time"
+
+	"github.com/coreos/go-etcd/etcd"
+)
+
+// handler manages the lock HTTP request.
+type handler struct {
+	*mux.Router
+	client string
+}
+
+// NewHandler creates an HTTP handler that can be registered on a router.
+func NewHandler(addr string) (http.Handler) {
+	h := &handler{
+		Router: mux.NewRouter(),
+		client: etcd.NewClient([]string{addr}),
+	}
+	h.HandleFunc("/{key:.+}", h.getLockHandler).Methods("GET")
+	h.HandleFunc("/{key:.+}", h.acquireLockHandler).Methods("PUT")
+	h.HandleFunc("/{key:.+}", h.releaseLockHandler).Methods("DELETE")
+}
+
+// getLockHandler retrieves whether a lock has been obtained for a given key.
+func (h *handler) getLockHandler(w http.ResponseWriter, req *http.Request) {
+	// TODO
+}
+
+// acquireLockHandler attempts to acquire a lock on the given key.
+// The lock is released when the connection is disconnected.
+func (h *handler) acquireLockHandler(w http.ResponseWriter, req *http.Request) {
+	// TODO
+}
+
+// releaseLockHandler forces the release of a lock on the given key.
+func (h *handler) releaseLockHandler(w http.ResponseWriter, req *http.Request) {
+	// TODO
+}

+ 35 - 0
mod/lock/handler_test.go

@@ -0,0 +1,35 @@
+package lock
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+// Ensure that a lock can be acquired and released.
+func TestModLockAcquire(t *testing.T) {
+	// TODO: Acquire lock.
+	// TODO: Check that it has been acquired.
+	// TODO: Release lock.
+	// TODO: Check that it has been released.
+}
+
+// Ensure that a lock can be acquired and another process is blocked until released.
+func TestModLockAcquireBlocked(t *testing.T) {
+	// TODO: Acquire lock with process #1.
+	// TODO: Acquire lock with process #2.
+	// TODO: Check that process #2 has not obtained lock.
+	// TODO: Release lock from process #1.
+	// TODO: Check that process #2 obtains the lock.
+	// TODO: Release lock from process #2.
+	// TODO: Check that no lock exists.
+}
+
+// Ensure that an unowned lock can be released by force.
+func TestModLockForceRelease(t *testing.T) {
+	// TODO: Acquire lock.
+	// TODO: Check that it has been acquired.
+	// TODO: Force release lock.
+	// TODO: Check that it has been released.
+	// TODO: Check that acquiring goroutine is notified that their lock has been released.
+}

+ 6 - 3
mod/mod.go

@@ -5,14 +5,17 @@ import (
 	"net/http"
 
 	"github.com/coreos/etcd/mod/dashboard"
+	"github.com/coreos/etcd/mod/lock"
 	"github.com/gorilla/mux"
 )
 
 var ServeMux *http.Handler
 
 func HttpHandler() (handler http.Handler) {
-	modMux := mux.NewRouter()
-	modMux.PathPrefix("/dashboard/").
+	r := mux.NewRouter()
+	r.PathPrefix("/dashboard/").
 		Handler(http.StripPrefix("/dashboard/", dashboard.HttpHandler()))
-	return modMux
+	r.PathPrefix("/lock/").
+		Handler(http.StripPrefix("/lock", lock.NewHandler()))
+	return r
 }