Changeset 15820 in project


Ignore:
Timestamp:
09/10/09 21:36:00 (10 years ago)
Author:
sjamaan
Message:

Add more tests, simplify node-info by returning an alist (thanks to Mario for advice on that)

Location:
release/4/estraier-client
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/4/estraier-client/estraier-client.scm

    r15778 r15820  
    4040
    4141(module estraier-client
    42   (node-info
    43    node-info-name node-info-label node-info-document-count
    44    node-info-word-count node-info-db-size node-info-admins node-info-guests
    45 
    46    node-cache-usage node-optimize node-sync document-uri->id
     42  (node-info node-cache-usage node-optimize node-sync document-uri->id
    4743   list-documents put-document update-document delete-document
    4844   get-document document-attribute document-keywords find-documents
     
    5046
    5147   master-shutdown master-sync master-backup master-rotate-log
    52    master-add-node master-delete-node master-clear-node
     48   master-nodes master-add-node master-delete-node master-clear-node
    5349   master-users master-add-user master-delete-user)
    5450
    5551(import chicken scheme)
    56 (use data-structures srfi-13 http-client uri-common intarweb)
     52(use data-structures extras ports srfi-1 srfi-13 http-client uri-common intarweb)
    5753
    5854;; TODO: Make reader optional. If it's optional, clear out input port and
     
    6056;; we can reverse the reader and writer again..  Not likely though.
    6157;; Use keywords (reader: foo writer: bar) instead?
    62 ;;
    63 ;; Perhaps we should make exec always construct a POST http/1.0 request
    64 ;; by default?  Or allow the method to be overridden....
    6558(define (exec uri reader #!optional writer)
    6659  ;; Estraier doesn't understand the preferred ";" separator
    6760  (parameterize ((form-urlencoded-separator "&"))
    6861    (call-with-input-request uri writer reader)))
     62
     63(define (discard-output in)
     64  (let loop ((line (read-line in)))
     65    (if (eof-object? line) (void) (loop (read-line in)))))
    6966
    7067;; Read one block of data. The Hyper Estraier API sends data out in blocks
     
    9087    (update-uri uri path: (append (uri-path uri) (list action)))))
    9188
    92 ;; TODO: Naming. "node-info-document-count" is a bit too long
    93 (define-record node-info
    94   name label document-count word-count db-size admins guests)
    95 
    9689(define (node-info node)
    9790  (exec
     
    10194            (admins (read-block in))
    10295            (guests (read-block in)))
    103        (apply (lambda (name label doc-count word-count size)
    104                 (make-node-info name label
    105                                 (string->number doc-count)
    106                                 (string->number word-count)
    107                                 (string->number size)
    108                                 admins guests))
    109               db-info)))))
     96       `((name . ,(first db-info))
     97         (label . ,(second db-info))
     98         (document-count . ,(string->number (third db-info)))
     99         (word-count . ,(string->number (fourth db-info)))
     100         (size . ,(string->number (fifth db-info)))
     101         (admins . ,admins)
     102         (guests . ,guests))))))
    110103
    111104(define (node-cache-usage node)
    112   (exec
    113    (node-uri node "cacheusage")
    114    (lambda (in)
    115      (string->number (read-line in)))))
     105  (string->number (exec (node-uri node "cacheusage") read-line)))
    116106
    117107(define (node-optimize node)
    118   (exec (node-uri node "optimize") read-lines)
    119   (void))
     108  (exec (node-uri node "optimize") discard-output))
    120109
    121110(define (node-sync node)
    122   (exec (node-uri node "sync") read-lines)
    123   (void))
     111  (exec (node-uri node "sync") discard-output))
    124112
    125113(define (list-documents node #!key max prev)
     
    169157(define (delete-document node #!key id uri)
    170158  (exec (node-uri node "out_doc" query: (id/uri->alist id uri))
    171         read-lines)
    172   (void))
     159        discard-output))
    173160
    174161;; XXX Perhaps URIs should be auto-converted to strings?
     
    307294
    308295(define (master-shutdown master)
    309   (exec (master-uri master "shutdown") read-line)
    310   (void))
     296  (exec (master-uri master "shutdown") discard-output))
    311297
    312298(define (master-sync master)
    313   (exec (master-uri master "sync") read-line)
    314   (void))
     299  (exec (master-uri master "sync") discard-output))
    315300
    316301(define (master-backup master)
    317   (exec (master-uri master "backup") read-line)
    318   (void))
     302  (exec (master-uri master "backup") discard-output))
    319303
    320304(define (master-rotate-log master)
    321   (exec (master-uri master "logrtt") read-line)
    322   (void))
     305  (exec (master-uri master "logrtt") discard-output))
    323306
    324307(define (master-nodes master)
     
    330313  (exec (master-uri master "nodeadd"
    331314                    query: `((name . ,node-name) (label . ,node-label)))
    332         read-lines))
     315        discard-output))
    333316
    334317(define (master-delete-node master node-name)
    335318  (exec (master-uri master "nodedel" query: `((name . ,node-name)))
    336         read-lines))
     319        discard-output))
    337320
    338321(define (master-clear-node master node-name)
    339322  (exec (master-uri master "nodeclr" query: `((name . ,node-name)))
    340         read-lines))
     323        discard-output))
    341324
    342325(define (master-users master)
     
    354337  (exec (make-request uri: (master-uri master "useradd")
    355338                      method: 'POST major: 1 minor: 0)
    356         read-lines
     339        discard-output
    357340        `((name . ,username) (passwd . ,password)
    358341          (flags . ,flags) (fname . ,fullname) (misc . ,description))))
    359342
    360343(define (master-delete-user master username)
    361   (exec (master-uri master "userdel" query: `((name . ,username))) read-lines))
     344  (exec (master-uri master "userdel" query: `((name . ,username)))
     345        discard-output))
    362346
    363347)
  • release/4/estraier-client/tests/run.scm

    r15778 r15820  
    11(load "../estraier-client.scm")
    22
    3 (use test)
     3(use test posix)
    44
     5(import estraier-client)
     6
     7;; This will start the server, we hope
     8(system "rm -rf masterdir")              ; Just in case
     9(system "estmaster init masterdir")
     10(printf "Waiting for estmaster to startup...")
     11(system "estmaster start -bg masterdir > /dev/null 2>&1")
     12(sleep 2)
     13
     14(define master-uri "http://admin:admin@localhost:1978/master")
     15(define node-uri "http://admin:admin@localhost:1978/node/testnode")
     16
     17(test-group "node master API"
     18  (test "Empty node list on init" '() (master-nodes master-uri))
     19  (test-error "Cannot connect with invalid credentials"
     20              (master-nodes "http://admin:invalid@localhost:1978/master"))
     21  (let ((nodes (begin (master-add-node master-uri "testnode")
     22                      (master-add-node master-uri "testnode2" "testlabel")
     23                      (master-nodes master-uri))))
     24    (test "After adding two nodes, they show up"
     25          '("testnode" "testnode2")
     26          (map car nodes))
     27    (test "Node label is accepted"
     28          '("testnode" "testlabel")
     29          (map cadr nodes)))
     30  (test "After deleting a node, it is gone"
     31        '("testnode")
     32        (begin (master-delete-node master-uri "testnode2")
     33               (map car (master-nodes master-uri))))
     34  (let ((users (begin (master-add-user master-uri "testuser" "password"
     35                                       fullname: "Joe testuser"
     36                                       description: "This is just a test")
     37                      (master-users master-uri))))
     38    (test "After adding a user, it shows up"
     39          '("admin" "testuser")
     40          (map car users)))
     41  (test "After deleting a user, it is gone"
     42        '("admin")
     43        (begin (master-delete-user master-uri "testuser")
     44               (map car (master-users master-uri)))))
     45
     46;; TODO: more in-depth tests of result values
     47
     48(test-group "node API"
     49  (let ((info (node-info node-uri)))
     50    (test "Node-info reports zero documents at first"
     51          0 (alist-ref 'document-count info))
     52    (test "Node-info reports zero words at first"
     53          0 (alist-ref 'word-count info))
     54    (test "Node-info reports no guests at first"
     55          '() (alist-ref 'guests info))
     56    (test "Node-info reports no admins at first"
     57          '() (alist-ref 'admins info)))
     58  (test "Cache usage starts out empty"
     59        0.0 (node-cache-usage node-uri))
     60  (test "Document list starts out empty"
     61        '() (list-documents node-uri))
     62  (test-error "Putting document without URI is an error"
     63              (put-document node-uri "This is just a test" '()))
     64  (test-assert "Putting documents with URIs succeed"
     65               (begin
     66                 (put-document node-uri "Just a test" '((@uri . "/test1")))
     67                 (put-document node-uri "Another test" '((@uri . "/test2")))
     68                 #t))
     69  (test "Nodes are listed"
     70        '("/test1" "/test2")
     71        (map (lambda (l) (alist-ref '@uri l)) (list-documents node-uri)))
     72  (test "Skipping a node works"
     73        '("/test2")
     74        (map (lambda (l) (alist-ref '@uri l))
     75             (list-documents node-uri prev: "/test1")))
     76  (test "Maximum result length is used"
     77        '("/test1")
     78        (map (lambda (l) (alist-ref '@uri l))
     79             (list-documents node-uri max: 1)))
     80
     81  (test "Clearing node results in empty document list"
     82        '()
     83        (begin (master-clear-node master-uri "testnode")
     84               (list-documents node-uri))))
     85
     86(test-group "cleanup"
     87 (test-assert "Clean shutdown" (begin (master-shutdown master-uri) #t))
     88 (test-error "After shutdown, server is unreachable" (master-nodes master-uri)))
     89(system "rm -rf masterdir")
Note: See TracChangeset for help on using the changeset viewer.