/// <summary> /// Read the value of an object as a string from the server /// for the given key. /// </summary> /// <remarks> /// For the case when the key corresponds to a list, this function /// gives a random value if the list has 'oneOf' attribute, /// else gives the values from the list in sequential order. /// </remarks> /// <param name="key">The key of the string to read.</param> /// <returns> /// The string value with the given key; null if not found. /// </returns> public string GetStringValue(string key) { FwkData data = ReadData(key); string res = null; if (data != null) { if (data.Kind == DataKind.String) { lock (((ICollection)m_dataIndexMap).SyncRoot) { int currentIndex; if (!m_dataIndexMap.TryGetValue(key, out currentIndex)) { currentIndex = 0; } if (currentIndex == 0) { res = data.Data1 as string; m_dataIndexMap[key] = 1; } } } else if (data.Kind == DataKind.List) { int len; List <string> dataList = data.Data1 as List <string>; if (dataList != null && (len = dataList.Count) > 0) { bool oneOf = false; if (data.Data2 != null && data.Data2 is bool) { oneOf = (bool)data.Data2; } if (oneOf) { res = dataList[Util.Rand(len)]; } else { lock (((ICollection)m_dataIndexMap).SyncRoot) { int currentIndex; if (!m_dataIndexMap.TryGetValue(key, out currentIndex)) { currentIndex = 0; } if (currentIndex < len) { res = dataList[currentIndex]; m_dataIndexMap[key] = currentIndex + 1; } } } } } // TODO: Deal with Range here. } return(res); }
/// <summary> /// Read the name of the region for the given key. /// </summary> /// <param name="key">The key of the region to read.</param> /// <returns>The name of the region.</returns> public string GetRegionName(string key) { FwkData data = ReadData(key); if (data != null && data.Kind == DataKind.Region) { return(data.Data1 as string); } return(null); }
/// <summary> /// Read the region attributes for the given key. /// </summary> /// <param name="key">The key of the region to read.</param> /// <returns>The attributes of the region.</returns> public Apache.Geode.Client.RegionAttributes <string, string> GetRegionAttributes(string key) { FwkData data = ReadData(key); if (data != null && data.Kind == DataKind.Region) { Apache.Geode.Client.AttributesFactory <string, string> af = new Apache.Geode.Client.AttributesFactory <string, string>(); Apache.Geode.Client.RegionAttributes <string, string> attrs = af.CreateRegionAttributes(); byte[] attrsArr = data.Data2 as byte[]; if (attrsArr != null && attrsArr.Length > 0) { Apache.Geode.Client.DataInput dinp = new Apache.Geode.Client.DataInput(attrsArr); attrs.FromData(dinp); } return(attrs); } return(null); }
public Apache.Geode.Client.Properties <string, string> GetPoolAttributes(string key) { FwkData data = ReadData(key); if (data != null && data.Kind == DataKind.Pool) { Apache.Geode.Client.Properties <string, string> prop = Apache.Geode.Client.Properties <string, string> .Create <string, string>(); //RegionAttributes attrs = af.CreateRegionAttributes(); byte[] attrsArr = data.Data2 as byte[]; if (attrsArr != null && attrsArr.Length > 0) { Apache.Geode.Client.DataInput dinp = new Apache.Geode.Client.DataInput(attrsArr); prop.FromData(dinp); } return(prop); } return(null); }
public static Dictionary <string, FwkData> ReadDataNodes(XmlNode node) { XmlNodeList xmlNodes = node.SelectNodes("data"); // Console.WriteLine("Total number of data nodes found = " + xmlNodes.Count); if (xmlNodes != null) { Dictionary <string, FwkData> dataNodes = new Dictionary <string, FwkData>(); foreach (XmlNode xmlNode in xmlNodes) { XmlAttribute tmpattr = xmlNode.Attributes["name"]; string name; if (tmpattr != null) { name = tmpattr.Value; } else { throw new IllegalArgException("The xml file passed has an unknown format"); } //Console.WriteLine("xmlNode.FirstChild.Name = " + xmlNode.FirstChild.Name); if (xmlNode.FirstChild == null || xmlNode.FirstChild.NodeType == XmlNodeType.Text) { object data = xmlNode.InnerText; // Console.WriteLine("Going to construct FwkData with data = " + data.ToString() + // " data2 = null " + " datakind = " + DataKind.String.ToString()); FwkData td = new FwkData(data, null, DataKind.String); dataNodes[name] = td; } else if (xmlNode.FirstChild.Name == "snippet") { string regionName; if (xmlNode.FirstChild.FirstChild.Name == "region") { XmlAttribute nameattr = xmlNode.FirstChild.FirstChild.Attributes["name"]; regionName = nameattr.Value; // Now collect the region atributes XmlNode attrnode = xmlNode.FirstChild.FirstChild.FirstChild; Apache.Geode.Client.Properties <string, string> rattr = Apache.Geode.Client.Properties <string, string> .Create <string, string>(); //AttributesFactory af = new AttributesFactory(); if (attrnode.Name == "region-attributes") { XmlAttributeCollection attrcoll = attrnode.Attributes; if (attrcoll != null) { foreach (XmlAttribute eachattr in attrcoll) { rattr.Insert(eachattr.Name, eachattr.Value); //SetThisAttribute(eachattr.Name, eachattr, af); } } if (attrnode.ChildNodes != null) { foreach (XmlNode tmpnode in attrnode.ChildNodes) { rattr.Insert(tmpnode.Name, tmpnode.Value); //SetThisAttribute(tmpnode.Name, tmpnode, af); } } Apache.Geode.Client.DataOutput dout = new Apache.Geode.Client.DataOutput(); //RegionAttributes rattr = af.CreateRegionAttributes(); rattr.ToData(dout); // Console.WriteLine("Going to construct FwkData with region = " + regionName + // " data2 = region attributes" + " datakind = " + DataKind.Region.ToString()); FwkData td = new FwkData(regionName, dout.GetBuffer(), DataKind.Region); dataNodes[name] = td; } else { throw new IllegalArgException("The xml file passed has an unknown format"); } } else if (xmlNode.FirstChild.FirstChild.Name == "pool") { XmlAttribute nameattr = xmlNode.FirstChild.FirstChild.Attributes["name"]; String poolName = nameattr.Value; // Now collect the pool atributes Apache.Geode.Client.Properties <string, string> prop = Apache.Geode.Client.Properties <string, string> .Create <string, string>(); XmlAttributeCollection attrcoll = xmlNode.FirstChild.FirstChild.Attributes; if (attrcoll != null) { foreach (XmlAttribute eachattr in attrcoll) { prop.Insert(eachattr.Name, eachattr.Value); } Apache.Geode.Client.DataOutput dout = new Apache.Geode.Client.DataOutput(); prop.ToData(dout); FwkData td = new FwkData(poolName, dout.GetBuffer(), DataKind.Pool); dataNodes[name] = td; } else { throw new IllegalArgException("The xml file passed has an unknown format"); } } else { throw new IllegalArgException("The xml file passed has an unknown format"); } } else if (xmlNode.FirstChild.Name == "list") { List <string> tmplist = new List <string>(); XmlNode listNode = xmlNode.FirstChild; if (listNode.FirstChild != null) { bool isOneOf = false; if (listNode.FirstChild.Name == "oneOf") { isOneOf = true; listNode = listNode.FirstChild; } XmlNodeList tmpListNodes = listNode.ChildNodes; foreach (XmlNode itemnode in tmpListNodes) { if (itemnode.Name == "item") { tmplist.Add(itemnode.InnerText); //Console.WriteLine("Adding the value " + itemnode.InnerText + " to the list"); } else { throw new IllegalArgException("The xml file passed has an unknown node " + itemnode.Name + " in data-list"); } } //Console.WriteLine("Going to construct FwkData list data: oneof = " + isOneOf.ToString() + " datakind = " + DataKind.List.ToString()); FwkData td = new FwkData(tmplist, isOneOf, DataKind.List); dataNodes[name] = td; } } else if (xmlNode.FirstChild.Name == "range") { XmlAttributeCollection rangeAttr = xmlNode.FirstChild.Attributes; string lowmarkstr = rangeAttr["low"].Value; string highmarkstr = rangeAttr["high"].Value; int lowmark = int.Parse(lowmarkstr); int highmark = int.Parse(highmarkstr); //Console.WriteLine("Going to construct FwkData Range datakind = " + DataKind.Range.ToString() + " high = " + highmark + " low = " + lowmark); FwkData td = new FwkData(lowmark, highmark, DataKind.Range); dataNodes[name] = td; } else if (xmlNode.FirstChild.Name == "oneof") { XmlNodeList itemlist = xmlNode.FirstChild.ChildNodes; List <string> opItemList = new List <string>(); foreach (XmlNode tmpitem in itemlist) { if (tmpitem.Name == "item") { opItemList.Add(tmpitem.InnerText); } } FwkData td = new FwkData(opItemList, true, DataKind.List); dataNodes[name] = td; } else { throw new IllegalArgException("The xml file passed has an unknown child: " + xmlNode.FirstChild.Name + " ; number of childnodes: " + (xmlNode.ChildNodes == null ? XmlNodeType.None : xmlNode.FirstChild.NodeType)); } } return(dataNodes); } return(null); }
public FwkTask(XmlNode node, string testName, UnitFnMethod <FwkTask, ClientBase> taskStartedHandler, UnitFnMethod <FwkTask, ClientBase, Exception> taskDoneHandler) { if (node == null) { throw new IllegalArgException("Null node for FwkTask constructor."); } #region Read the attributes string taskName = string.Empty; string containerStr = null; string methodName = null; string typeAttrib = null; m_timesToRun = 1; m_threadCount = 1; m_parallel = false; m_timeoutMillis = DefaultTimeoutMillis; m_continueOnError = false; m_class = null; XmlAttributeCollection xmlAttribs = node.Attributes; if (xmlAttribs != null) { foreach (XmlAttribute xmlAttrib in xmlAttribs) { switch (xmlAttrib.Name) { case NameAttrib: taskName = xmlAttrib.Value; break; case ArgTypes: typeAttrib = xmlAttrib.Value; break; case ContainerAttrib: containerStr = xmlAttrib.Value; break; case MethodAttrib: methodName = xmlAttrib.Value; break; case TimesToRunAttrib: m_timesToRun = XmlNodeReaderWriter.String2Int(xmlAttrib.Value, 1); break; case ThreadCountAttrib: m_threadCount = XmlNodeReaderWriter.String2Int(xmlAttrib.Value, 1); break; case ParallelAttrib: m_parallel = XmlNodeReaderWriter.String2Bool(xmlAttrib.Value, false); break; case WaitTimeAttrib: m_timeoutMillis = XmlNodeReaderWriter.String2Seconds(xmlAttrib.Value, DefaultTimeoutMillis) * 1000; break; case ContinueAttrib: m_continueOnError = XmlNodeReaderWriter.String2Bool(xmlAttrib.Value, false); break; case ClassAttrib: m_class = xmlAttrib.Value; break; default: throw new IllegalArgException("Unknown attribute '" + xmlAttrib.Name + "' found in '" + node.Name + "' node."); } } } int taskNum = 1; if (m_GlobalTaskNames.ContainsKey(taskName)) { taskNum = m_GlobalTaskNames[taskName] + 1; } m_GlobalTaskNames[taskName] = taskNum; taskName += '_' + taskNum.ToString(); #endregion m_timeBomb = new TimeBomb(); m_timeBomb.TaskName = testName + '.' + taskName; #region Create a delegate by loading the assembly Assembly loadAssmb = null; string typeName = null; if (containerStr != null && methodName != null) { object inst = null; int dotIndx; if ((dotIndx = containerStr.IndexOf('.')) < 0) { Type myType = this.GetType(); loadAssmb = myType.Assembly; typeName = myType.Namespace + '.' + containerStr; Util.Log(Util.LogLevel.Info, "Assembly {0} loaded and typename is {1}", loadAssmb, typeName); } else { string assmbName = containerStr.Substring(0, dotIndx); if (!m_AssemblyMap.TryGetValue(assmbName, out loadAssmb)) { try { loadAssmb = Assembly.Load(assmbName); Util.Log(Util.LogLevel.Info, "Assembly {0} loaded ", assmbName); } catch (Exception e) { throw new IllegalArgException("Cannot load assembly '" + assmbName + "' for task: " + m_timeBomb.TaskName + " exception: " + e); } m_AssemblyMap.Add(assmbName, loadAssmb); } typeName = containerStr.Substring(dotIndx + 1); } //string typeAttrib; if (loadAssmb != null) { if (typeAttrib == null) { inst = loadAssmb.CreateInstance(typeName, true); } else { //typeAttrib = "Apache.Geode.Client.Tests.ArrayOfByte,Apache.Geode.Client.Tests.ArrayOfByte"; //typeAttrib = "System.int,System.Int32"; string[] typeNames = typeAttrib.Split(','); string mangledName = typeName + "`" + typeNames.Length.ToString(); //Type type = loadAssmb.GetType(mangledName, true, true); Type[] types = new Type[typeNames.Length]; for (int index = 0; index < typeNames.Length; ++index) { string typName = typeNames[index].Trim(); if (typName == "int" || typName == "Int32" || typName == "string" || typName == "String" || typName == "byte[]" || typName == "Byte[]" || typName == "string[]" || typName == "String[]" || typName == "Object" || typName == "object") { if (typName.Equals("int")) { typName = "Int32"; } else if (typName.Equals("string")) { typName = "String"; } else if (typName.Equals("string[]")) { typName = "String[]"; } else if (typName.Equals("byte[]")) { typName = "Byte[]"; } else if (typName.Equals("object")) { typName = "Object"; } typName = "System." + typName; //Util.Log("rjk: FwkTask: typeAttrib 33 argname {0}", typName); types[index] = Type.GetType(typName.Trim()); //Util.Log("rjk: FwkTask: typeAttrib 34 argname {0}", typName); } else { typName = "Apache.Geode.Client.Tests." + typName; types[index] = loadAssmb.GetType(typName.Trim(), true, true); //Util.Log("rjk: FwkTask: typeAttrib for userobject 34 argname {0}", typName); } } Type type = loadAssmb.GetType(mangledName, true, true).MakeGenericType(types); inst = type.GetConstructor(System.Type.EmptyTypes).Invoke(null); } } if (inst != null) { try { MethodInfo mInfo = inst.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Instance); m_delegate = Delegate.CreateDelegate(typeof(UnitFnMethod), inst, mInfo, true); } catch (Exception ex) { throw new IllegalArgException( "Exception while creating delegate [" + methodName + "]: " + ex); } m_pushTaskNameDelegate = Delegate.CreateDelegate( typeof(UnitFnMethod <string>), inst, PushTaskNameMethod, true); m_endDelegate = Delegate.CreateDelegate( typeof(UnitFnMethod), inst, EndTaskMethod, true); } } if (m_delegate == null) { throw new IllegalArgException("Cannot create delegate '" + methodName + "' for task: " + m_timeBomb.TaskName); } #endregion #region Add the clients m_clients = new List <string>(); m_clientGroup = new ClientGroup(false); m_clients = FwkClient.ReadClientNames(node, false); List <ClientBase> clients = FwkClient.GetClients(m_clients); m_clientGroup.Add(clients); m_timeBomb.AddClients(new ClientBase[] { m_clientGroup }); m_taskStartedHandler = taskStartedHandler; m_taskDoneHandler = taskDoneHandler; int clientCount = m_clients.Count; #endregion #region Add any data m_localDataNames = new List <string>(); Dictionary <string, FwkData> data = FwkData.ReadDataNodes(node); // Task specific data is written as <taskname>.<key> to avoid // overwriting the global data, since that needs to be preserved // across tasks. if (m_threadCount > 1) { // We shall treat 'threadCount' and 'numThreads' as equivalent, // i.e. if 'threadCount' is defined for a task then 'numThreads' // shall also be written as data. foreach (string threadKey in ThreadCountKeys) { m_localDataNames.Add(threadKey); Util.BBSet(Name, threadKey, new FwkData(m_threadCount.ToString(), null, DataKind.String)); } } if (clientCount > 0) { // Overwrite the clientCount (if any) with the actual value. Util.BBSet(Name, ClientCountKey, new FwkData(clientCount.ToString(), null, DataKind.String)); } foreach (KeyValuePair <string, FwkData> pair in data) { m_localDataNames.Add(pair.Key); Util.BBSet(Name, pair.Key, pair.Value); } #endregion }