You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

442 lines
10 KiB

3 years ago
3 years ago
  1. // Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.
  2. package container
  3. import (
  4. "github.com/go-xorm/xorm"
  5. "reflect"
  6. "testing"
  7. )
  8. func FactoryWithoutResult() {
  9. }
  10. type TestElement interface {
  11. }
  12. type TestElementImplement struct {
  13. }
  14. func FactoryOfNullTestElement() TestElement {
  15. return nil
  16. }
  17. func FactoryOfElementTestInterfaceWithContent(string) TestElement {
  18. return &TestElementImplement{}
  19. }
  20. func FactoryOfTestElementImplement() *TestElementImplement {
  21. return &TestElementImplement{}
  22. }
  23. func FactoryOfTestElement() TestElement {
  24. return &TestElementImplement{}
  25. }
  26. func FactoryWithTooManyResults() (TestElement, TestService) {
  27. return &TestElementImplement{}, &TestServiceImplement{}
  28. }
  29. var testElementType = reflect.TypeOf((*TestElement)(nil)).Elem()
  30. var elementInstance = Instance(reflect.ValueOf(FactoryOfTestElement()))
  31. type TestService interface {
  32. String() (string, error)
  33. StringWithContext(*RequestContext) (string, error)
  34. StringNotFirstContext(string, *RequestContext) (string, error)
  35. String2() (string, string, error)
  36. String3(*RequestContext, *RequestContext) error
  37. NoError() string
  38. }
  39. type TestToString interface {
  40. String() string
  41. }
  42. type TestServiceImplement struct {
  43. content string
  44. }
  45. func (impl *TestServiceImplement) String() (string, error) {
  46. return impl.content, nil
  47. }
  48. func (impl *TestServiceImplement) StringWithContext(_ *RequestContext) (string, error) {
  49. return impl.content, nil
  50. }
  51. func (impl *TestServiceImplement) StringNotFirstContext(_ string, _ *RequestContext) (string, error) {
  52. return "", nil
  53. }
  54. func (impl *TestServiceImplement) String2() (string, string, error) {
  55. return impl.content, impl.content, nil
  56. }
  57. func (impl *TestServiceImplement) String3(_ *RequestContext, _ *RequestContext) error {
  58. return nil
  59. }
  60. func (impl *TestServiceImplement) NoError() string {
  61. return impl.content
  62. }
  63. func FactoryOfTestService() TestService {
  64. return &TestServiceImplement{}
  65. }
  66. func FactoryOfServiceTestInterfaceWithContent(content string) TestService {
  67. return &TestServiceImplement{content}
  68. }
  69. type TestServiceImplementWithContext struct {
  70. context *SessionContext
  71. }
  72. func (impl *TestServiceImplementWithContext) String() (string, error) {
  73. return "", nil
  74. }
  75. func (impl *TestServiceImplementWithContext) StringWithContext(_ *RequestContext) (string, error) {
  76. return "", nil
  77. }
  78. func (impl *TestServiceImplementWithContext) StringNotFirstContext(_ string, _ *RequestContext) (string, error) {
  79. return "", nil
  80. }
  81. func (impl *TestServiceImplementWithContext) String2() (string, string, error) {
  82. return "", "", nil
  83. }
  84. func (impl *TestServiceImplementWithContext) String3(_ *RequestContext, _ *RequestContext) error {
  85. return nil
  86. }
  87. func (impl *TestServiceImplementWithContext) NoError() string {
  88. return ""
  89. }
  90. func (impl *TestServiceImplementWithContext) SetContext(context *SessionContext) {
  91. impl.context = context
  92. }
  93. type TestWithTooManyInSetContext interface {
  94. SetContext(*SessionContext, string)
  95. }
  96. type TestWithTooManyInSetContextImplement struct {
  97. context *SessionContext
  98. }
  99. func (impl *TestWithTooManyInSetContextImplement) SetContext(_ *SessionContext, _ string) {
  100. }
  101. func FactoryOfTestWithTooManyInSetContext() TestWithTooManyInSetContext {
  102. return &TestWithTooManyInSetContextImplement{}
  103. }
  104. type TestWithReturnValueSetContext interface {
  105. SetContext(*SessionContext) error
  106. }
  107. type TestWithReturnValueSetContextImplement struct {
  108. context *SessionContext
  109. }
  110. func (impl *TestWithReturnValueSetContextImplement) SetContext(context *SessionContext) error {
  111. impl.context = context
  112. return nil
  113. }
  114. func FactoryOfTestWithReturnValueSetContext() TestWithReturnValueSetContext {
  115. return &TestWithReturnValueSetContextImplement{}
  116. }
  117. type TestWithErrorInSetContext interface {
  118. SetContext(string)
  119. }
  120. type TestWithErrorInSetContextImplement struct {
  121. context *SessionContext
  122. }
  123. func (impl *TestWithErrorInSetContextImplement) SetContext(_ string) {
  124. }
  125. func FactoryOfTestWithErrorInSetContext() TestWithErrorInSetContext {
  126. return &TestWithErrorInSetContextImplement{}
  127. }
  128. type TestWithNoInSetContext interface {
  129. SetContext()
  130. }
  131. type TestWithNoInSetContextImplement struct {
  132. context *SessionContext
  133. }
  134. func (impl *TestWithNoInSetContextImplement) SetContext() {
  135. }
  136. func FactoryOfTestWithNoInSetContext() TestWithNoInSetContext {
  137. return &TestWithNoInSetContextImplement{}
  138. }
  139. type TestToStringImplement struct {
  140. context *SessionContext
  141. }
  142. func (impl TestToStringImplement) String() string {
  143. return ""
  144. }
  145. func FactoryOfTestServiceWithContext() TestService {
  146. return &TestServiceImplementWithContext{}
  147. }
  148. func FactoryOfTestServiceImplementWithContext() *TestServiceImplementWithContext {
  149. return &TestServiceImplementWithContext{}
  150. }
  151. func FactoryOfTestServiceImplementWithContextStruct() TestServiceImplementWithContext {
  152. return TestServiceImplementWithContext{}
  153. }
  154. func FactoryOfNullTestService() TestService {
  155. return nil
  156. }
  157. func FactoryOfNullTestServiceImplementWithContext() *TestServiceImplementWithContext {
  158. return nil
  159. }
  160. func FactoryOfInt() *int {
  161. intValue := 123
  162. return &intValue
  163. }
  164. func FactoryOfTestToString() TestToString {
  165. return TestToStringImplement{}
  166. }
  167. var testServiceType = reflect.TypeOf((*TestService)(nil)).Elem()
  168. var serviceInstance = Instance(reflect.ValueOf(FactoryOfTestService()))
  169. type TestCallerBrokerBuilderImplementWithError struct {
  170. checkingError error
  171. buildingError error
  172. }
  173. func (impl *TestCallerBrokerBuilderImplementWithError) Check(_ Method) error {
  174. return impl.checkingError
  175. }
  176. func (impl *TestCallerBrokerBuilderImplementWithError) Build(_ *SessionContext, _ Caller) (Caller, error) {
  177. return nil, impl.buildingError
  178. }
  179. type TestCallerBrokerBuilderImplement struct {
  180. }
  181. func (impl *TestCallerBrokerBuilderImplement) Check(_ Method) error {
  182. return nil
  183. }
  184. func (impl *TestCallerBrokerBuilderImplement) Build(_ *SessionContext, caller Caller) (Caller, error) {
  185. return caller, nil
  186. }
  187. type TransactionHandlerMock struct {
  188. closeNumberOfTimes int
  189. beginNumberOfTimes int
  190. beginError error
  191. commitNumberOfTimes int
  192. commitError error
  193. session *xorm.Session
  194. }
  195. func (mock *TransactionHandlerMock) Close() {
  196. mock.closeNumberOfTimes++
  197. }
  198. func (mock *TransactionHandlerMock) Begin() error {
  199. mock.beginNumberOfTimes++
  200. return mock.beginError
  201. }
  202. func (mock *TransactionHandlerMock) Commit() error {
  203. mock.commitNumberOfTimes++
  204. return mock.commitError
  205. }
  206. func (mock *TransactionHandlerMock) Session() *xorm.Session {
  207. return mock.session
  208. }
  209. type TransactionHandlerFactoryMock struct {
  210. handler *TransactionHandlerMock
  211. createError error
  212. }
  213. // @Reference LAPP_GAAS_GFrame_BACKEND/container/TransactionHandlerFactory.Create
  214. func (mock *TransactionHandlerFactoryMock) Create() (TransactionHandler, error) {
  215. return mock.handler, mock.createError
  216. }
  217. var transactionHandlerFactoryMock TransactionHandlerFactory = &TransactionHandlerFactoryMock{}
  218. func TestContainer_ParseFactory(t *testing.T) {
  219. _, _ = parseFactory(FactoryOfTestElement)
  220. }
  221. func TestContainer_ParseFactory_NullError(t *testing.T) {
  222. defer func() {
  223. if err := recover(); err != "工厂不能为空!" {
  224. t.Fatalf("意外错误:%s", err)
  225. }
  226. }()
  227. _, _ = parseFactory(nil)
  228. t.Fatalf("意外的没有错误!")
  229. }
  230. func TestContainer_ParseFactory_FactoryTypeError(t *testing.T) {
  231. defer func() {
  232. if err := recover(); err != "工厂必须是func!" {
  233. t.Fatalf("意外错误:%s", err)
  234. }
  235. }()
  236. _, _ = parseFactory(reflect.TypeOf(FactoryOfTestElement()))
  237. t.Fatalf("意外的没有错误!")
  238. }
  239. func TestContainer_ParseFactory_ParameterCountError(t *testing.T) {
  240. defer func() {
  241. if err := recover(); err != "工厂不能有参数!" {
  242. t.Fatalf("意外错误:%s", err)
  243. }
  244. }()
  245. _, _ = parseFactory(FactoryOfElementTestInterfaceWithContent)
  246. t.Fatalf("意外的没有错误!")
  247. }
  248. func TestContainer_ParseFactory_NoReturnValueError(t *testing.T) {
  249. defer func() {
  250. if err := recover(); err != "工厂必值需有返回值!" {
  251. t.Fatalf("意外错误:%s", err)
  252. }
  253. }()
  254. _, _ = parseFactory(FactoryWithoutResult)
  255. t.Fatalf("意外的没有错误!")
  256. }
  257. func TestContainer_ParseFactory_TooManyResultsError(t *testing.T) {
  258. defer func() {
  259. if err := recover(); err != "工厂只能有一个返回值!" {
  260. t.Fatalf("意外错误:%s", err)
  261. }
  262. }()
  263. _, _ = parseFactory(FactoryWithTooManyResults)
  264. t.Fatalf("意外的没有错误!")
  265. }
  266. func TestContainer_ParseFactory_ReturnValueTypeError(t *testing.T) {
  267. defer func() {
  268. if err := recover(); err != "工厂的返回值必需是interface!" {
  269. t.Fatalf("意外错误:%s", err)
  270. }
  271. }()
  272. _, _ = parseFactory(FactoryOfTestElementImplement)
  273. t.Fatalf("意外的没有错误!")
  274. }
  275. func call1(_ []reflect.Value) []reflect.Value {
  276. return []reflect.Value{reflect.ValueOf(1)}
  277. }
  278. func call2(_ []reflect.Value) []reflect.Value {
  279. return []reflect.Value{reflect.ValueOf(2)}
  280. }
  281. func TestContainer_Caller_Equal_NullCaller1AndCaller2(t *testing.T) {
  282. if !CallerEqual(nil, nil) {
  283. t.Fatalf("意外的不相等!")
  284. }
  285. }
  286. func TestContainer_Caller_Equal_NullCaller1(t *testing.T) {
  287. if CallerEqual(nil, call1) {
  288. t.Fatalf("意外的相等!")
  289. }
  290. }
  291. func TestContainer_Caller_Equal_NullCaller2(t *testing.T) {
  292. if CallerEqual(call1, nil) {
  293. t.Fatalf("意外的相等!")
  294. }
  295. }
  296. func TestContainer_Caller_Equal_NotNull(t *testing.T) {
  297. var caller1 = call1
  298. var caller2 = call1
  299. var caller3 = call2
  300. if !CallerEqual(caller1, caller2) {
  301. t.Fatalf("意外的不相等!")
  302. }
  303. if CallerEqual(caller2, caller3) {
  304. t.Fatalf("意外的相等!")
  305. }
  306. call3 := func(_ []reflect.Value) []reflect.Value {
  307. return []reflect.Value{reflect.ValueOf(3)}
  308. }
  309. call4 := func(_ []reflect.Value) []reflect.Value {
  310. return []reflect.Value{reflect.ValueOf(4)}
  311. }
  312. var caller4 = call3
  313. var caller5 = call3
  314. var caller6 = call4
  315. if !CallerEqual(caller4, caller5) {
  316. t.Fatalf("意外的不相等!")
  317. }
  318. if CallerEqual(caller5, caller6) {
  319. t.Fatalf("意外的相等!")
  320. }
  321. }
  322. type Synchronizer interface {
  323. Awake()
  324. Wait()
  325. }
  326. type SynchronizerImplement struct {
  327. core chan interface{}
  328. }
  329. func NewSynchronizer() Synchronizer {
  330. return &SynchronizerImplement{make(chan interface{}, 1)}
  331. }
  332. func (impl SynchronizerImplement) Awake() {
  333. impl.core <- 1
  334. }
  335. func (impl SynchronizerImplement) Wait() {
  336. <-impl.core
  337. }