github.com/jbking/gohan@v0.0.0-20151217002006-b41ccf1c2a96/extension/v8/v8_test.go (about)

     1  // Copyright (C) 2015 NTT Innovation Institute, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // +build v8
    17  // Copyright (C) 2015 NTT Innovation Institute, Inc.
    18  //
    19  // Licensed under the Apache License, Version 2.0 (the "License");
    20  // you may not use this file except in compliance with the License.
    21  // You may obtain a copy of the License at
    22  //
    23  //    http://www.apache.org/licenses/LICENSE-2.0
    24  //
    25  // Unless required by applicable law or agreed to in writing, software
    26  // distributed under the License is distributed on an "AS IS" BASIS,
    27  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    28  // implied.
    29  // See the License for the specific language governing permissions and
    30  // limitations under the License.
    31  
    32  package v8
    33  
    34  import (
    35  	_ "github.com/onsi/ginkgo"
    36  	_ "github.com/onsi/gomega"
    37  	"io/ioutil"
    38  	"log"
    39  	"os"
    40  	"testing"
    41  )
    42  
    43  type ExampleObject struct {
    44  	count float64
    45  }
    46  
    47  func (object *ExampleObject) Method(context map[string]interface{}, count float64) float64 {
    48  	log.Printf("called %s", count)
    49  	object.count += count
    50  	log.Println(object.count)
    51  	return object.count
    52  }
    53  
    54  func (object *ExampleObject) Method2(context map[string]interface{}) {
    55  	log.Printf("called %s", context["fp"])
    56  }
    57  
    58  func TestV8(t *testing.T) {
    59  	env := NewEnvironment()
    60  
    61  	example := &ExampleObject{count: 0}
    62  	env.rpc.RegistObject("example", example)
    63  
    64  	testJSCode := `
    65  var example = {
    66    "count": 0,
    67    "cast": function(contextID, num){
    68      this.count += num;
    69    },
    70    "call": function(contextID, num){
    71      this.count += num;
    72      v8rpc.cast("example", "Method", [contextID, 1]);
    73      v8rpc.call("example", "Method", [contextID, 2], function(reply){
    74        example.count += reply;
    75      });
    76      return this.count;
    77    },
    78  };
    79  v8rpc.register_object("example", example);
    80  
    81  
    82  	`
    83  	err := env.Load("test_js_code", testJSCode)
    84  	if err != nil {
    85  		log.Fatal("Failed to load %s", err)
    86  	}
    87  	context := map[string]interface{}{}
    88  	contextID := "context_id"
    89  	env.rpc.contexts[contextID] = context
    90  	defer delete(env.rpc.contexts, contextID)
    91  	env.rpc.Cast("example", "cast", []interface{}{contextID, 4})
    92  	env.rpc.Call("example", "call", []interface{}{contextID, 8}, func(reply interface{}) {
    93  		log.Printf("%v", reply)
    94  	})
    95  
    96  	reply := env.rpc.BlockCall("example", "call", []interface{}{contextID, 16})
    97  	if reply.(float64) != 37 {
    98  		t.Log(reply)
    99  		t.Fail()
   100  	}
   101  	if example.count != 6 {
   102  		t.Fail()
   103  	}
   104  }
   105  
   106  func Test_Object(t *testing.T) {
   107  	env := NewEnvironment()
   108  
   109  	example := &ExampleObject{count: 0}
   110  	env.rpc.RegistObject("example", example)
   111  
   112  	testJSCode := `
   113  var example = {
   114    "call": function(contextID){
   115      v8rpc.cast("example", "Method2", [contextID]);
   116      return contextID
   117    },
   118  };
   119  v8rpc.register_object("example", example);
   120  	`
   121  	err := env.Load("test_js_code", testJSCode)
   122  	if err != nil {
   123  		log.Fatal("Failed to load %s", err)
   124  	}
   125  	fp, _ := os.Open("./test.txt")
   126  	context := map[string]interface{}{}
   127  	context["fp"] = fp
   128  	contextID := "context_id"
   129  	env.rpc.contexts[contextID] = context
   130  	defer delete(env.rpc.contexts, contextID)
   131  	env.rpc.Call("example", "call", []interface{}{contextID}, func(reply interface{}) {
   132  		log.Printf("%v", reply)
   133  	})
   134  }